This repository has been archived by the owner on Apr 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added token response persistence in the Redis store (#12)
- Loading branch information
Showing
28 changed files
with
821 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Copyright 2024 Tetrate | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
include ../suite.mk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Copyright 2024 Tetrate | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
version: "3.9" | ||
|
||
services: | ||
redis: | ||
image: redis:7.2.4 | ||
platform: linux/${ARCH:-amd64} | ||
ports: | ||
- "6379:6379" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright 2024 Tetrate | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package mock | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/lestrrat-go/jwx/jwa" | ||
"github.com/lestrrat-go/jwx/jwt" | ||
"github.com/redis/go-redis/v9" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/tetrateio/authservice-go/internal/oidc" | ||
) | ||
|
||
const redisURL = "redis://localhost:6379" | ||
|
||
func TestRedisTokenResponse(t *testing.T) { | ||
opts, err := redis.ParseURL(redisURL) | ||
require.NoError(t, err) | ||
client := redis.NewClient(opts) | ||
|
||
store, err := oidc.NewRedisStore(&oidc.Clock{}, client, 0, 1*time.Minute) | ||
require.NoError(t, err) | ||
|
||
ctx := context.Background() | ||
|
||
tr, err := store.GetTokenResponse(ctx, "s1") | ||
require.NoError(t, err) | ||
require.Nil(t, tr) | ||
|
||
// Create a session and verify it's added and accessed time | ||
tr = &oidc.TokenResponse{ | ||
IDToken: newToken(), | ||
AccessToken: newToken(), | ||
AccessTokenExpiresAt: time.Now().Add(30 * time.Minute), | ||
} | ||
require.NoError(t, store.SetTokenResponse(ctx, "s1", tr)) | ||
|
||
// Verify we can retrieve the token | ||
got, err := store.GetTokenResponse(ctx, "s1") | ||
require.NoError(t, err) | ||
// The testify library doesn't properly compare times, so we need to do it manually | ||
// then set the times in the returned object so that we can compare the rest of the | ||
// fields normally | ||
require.True(t, tr.AccessTokenExpiresAt.Equal(got.AccessTokenExpiresAt)) | ||
got.AccessTokenExpiresAt = tr.AccessTokenExpiresAt | ||
require.Equal(t, tr, got) | ||
|
||
// Verify that the token TTL has been set | ||
ttl := client.TTL(ctx, "s1").Val() | ||
require.Greater(t, ttl, time.Duration(0)) | ||
} | ||
|
||
func newToken() string { | ||
token, _ := jwt.NewBuilder(). | ||
Issuer("authservice"). | ||
Subject("user"). | ||
Expiration(time.Now().Add(time.Hour)). | ||
Build() | ||
signed, _ := jwt.Sign(token, jwa.HS256, []byte("key")) | ||
return string(signed) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Copyright 2024 Tetrate | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# Force run of the e2e tests | ||
E2E_TEST_OPTS ?= -count=1 | ||
|
||
export ARCH := $(shell uname -m) | ||
ifeq ($(ARCH),x86_64) | ||
export ARCH := amd64 | ||
endif | ||
|
||
.PHONY: e2e | ||
e2e: e2e-pre | ||
@$(MAKE) e2e-test e2e-post | ||
|
||
.PHONY: e2e-test | ||
e2e-test: | ||
@go test $(E2E_TEST_OPTS) ./... || ( $(MAKE) e2e-post-error; exit 1 ) | ||
|
||
.PHONY: e2e-pre | ||
e2e-pre: | ||
@docker compose up --detach --wait --force-recreate --remove-orphans || ($(MAKE) e2e-post-error; exit 1) | ||
|
||
.PHONY: e2e-post | ||
e2e-post: | ||
@docker compose down | ||
|
||
.PHONY: e2e-post-error | ||
e2e-post-error: capture-logs | ||
|
||
.PHONY: capture-logs | ||
capture-logs: | ||
@mkdir -p ./logs | ||
@docker compose logs > logs/docker-compose-logs.log | ||
|
||
.PHONY: clean | ||
clean: | ||
@rm -rf ./logs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.