Skip to content

Commit 044b025

Browse files
committed
enhancement: add access_token_expiry_time env variable
1 parent 1b387f7 commit 044b025

File tree

19 files changed

+163
-13
lines changed

19 files changed

+163
-13
lines changed

dashboard/src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export const LOGO_URL =
22
'https://user-images.githubusercontent.com/6964334/147834043-fc384cab-e7ca-40f8-9663-38fc25fd5f3a.png';
33

44
export const TextInputType = {
5+
ACCESS_TOKEN_EXPIRY_TIME: 'ACCESS_TOKEN_EXPIRY_TIME',
56
CLIENT_ID: 'CLIENT_ID',
67
GOOGLE_CLIENT_ID: 'GOOGLE_CLIENT_ID',
78
GITHUB_CLIENT_ID: 'GITHUB_CLIENT_ID',

dashboard/src/graphql/queries/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export const EnvVariablesQuery = `
5252
DATABASE_NAME,
5353
DATABASE_TYPE,
5454
DATABASE_URL,
55+
ACCESS_TOKEN_EXPIRY_TIME,
5556
}
5657
}
5758
`;

dashboard/src/pages/Environment.tsx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ interface envVarTypes {
7272
DATABASE_NAME: string;
7373
DATABASE_TYPE: string;
7474
DATABASE_URL: string;
75+
ACCESS_TOKEN_EXPIRY_TIME: string;
7576
}
7677

7778
export default function Environment() {
@@ -118,6 +119,7 @@ export default function Environment() {
118119
DATABASE_NAME: '',
119120
DATABASE_TYPE: '',
120121
DATABASE_URL: '',
122+
ACCESS_TOKEN_EXPIRY_TIME: '',
121123
});
122124

123125
const [fieldVisibility, setFieldVisibility] = React.useState<
@@ -626,19 +628,35 @@ export default function Environment() {
626628
</Stack>
627629
<Divider marginTop="2%" marginBottom="2%" />
628630
<Text fontSize="md" paddingTop="2%" fontWeight="bold">
629-
Custom Access Token Scripts
631+
Access Token
630632
</Text>
631633
<Stack spacing={6} padding="2% 0%">
632634
<Flex>
633-
<Center w="100%">
635+
<Flex w="30%" justifyContent="start" alignItems="center">
636+
<Text fontSize="sm">Access Token Expiry Time:</Text>
637+
</Flex>
638+
<Flex w="70%">
634639
<InputField
640+
variables={envVariables}
641+
setVariables={setEnvVariables}
642+
inputType={TextInputType.ACCESS_TOKEN_EXPIRY_TIME}
643+
placeholder="0h15m0s"
644+
/>
645+
</Flex>
646+
</Flex>
647+
<Flex>
648+
<Flex w="30%" justifyContent="start" alignItems="center">
649+
<Text fontSize="sm">Custom Access Token Scripts:</Text>
650+
</Flex>
651+
<Flex w="70%">
652+
<InputField
635653
variables={envVariables}
636654
setVariables={setEnvVariables}
637655
inputType={TextAreaInputType.CUSTOM_ACCESS_TOKEN_SCRIPT}
638656
placeholder="Add script here"
639657
minH="25vh"
640658
/>
641-
</Center>
659+
</Flex>
642660
</Flex>
643661
</Stack>
644662
<Divider marginTop="2%" marginBottom="2%" />

server/constants/env.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ const (
2121
// EnvKeyPort key for env variable PORT
2222
EnvKeyPort = "PORT"
2323

24+
// EnvKeyAccessTokenExpiryTime key for env variable ACCESS_TOKEN_EXPIRY_TIME
25+
EnvKeyAccessTokenExpiryTime = "ACCESS_TOKEN_EXPIRY_TIME"
2426
// EnvKeyAdminSecret key for env variable ADMIN_SECRET
2527
EnvKeyAdminSecret = "ADMIN_SECRET"
2628
// EnvKeyDatabaseType key for env variable DATABASE_TYPE

server/env/env.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ func InitAllEnv() error {
120120
}
121121
}
122122

123+
if envData.StringEnv[constants.EnvKeyAccessTokenExpiryTime] == "" {
124+
envData.StringEnv[constants.EnvKeyAccessTokenExpiryTime] = os.Getenv(constants.EnvKeyAccessTokenExpiryTime)
125+
}
126+
123127
if envData.StringEnv[constants.EnvKeyAdminSecret] == "" {
124128
envData.StringEnv[constants.EnvKeyAdminSecret] = os.Getenv(constants.EnvKeyAdminSecret)
125129
}

server/graph/generated/generated.go

Lines changed: 52 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/graph/model/models_gen.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/graph/schema.graphqls

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ type Response {
8585
}
8686

8787
type Env {
88+
ACCESS_TOKEN_EXPIRY_TIME: String
8889
ADMIN_SECRET: String
8990
DATABASE_NAME: String!
9091
DATABASE_URL: String!
@@ -125,6 +126,7 @@ type Env {
125126
}
126127

127128
input UpdateEnvInput {
129+
ACCESS_TOKEN_EXPIRY_TIME: String
128130
ADMIN_SECRET: String
129131
CUSTOM_ACCESS_TOKEN_SCRIPT: String
130132
OLD_ADMIN_SECRET: String

server/handlers/authorize.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"net/http"
55
"strconv"
66
"strings"
7+
"time"
78

89
"github.com/authorizerdev/authorizer/server/constants"
910
"github.com/authorizerdev/authorizer/server/cookie"
@@ -276,7 +277,11 @@ func AuthorizeHandler() gin.HandlerFunc {
276277
sessionstore.SetState(authToken.FingerPrintHash, authToken.FingerPrint+"@"+user.ID)
277278
sessionstore.SetState(authToken.AccessToken.Token, authToken.FingerPrint+"@"+user.ID)
278279
cookie.SetSession(gc, authToken.FingerPrintHash)
279-
expiresIn := int64(1800)
280+
281+
expiresIn := authToken.AccessToken.ExpiresAt - time.Now().Unix()
282+
if expiresIn <= 0 {
283+
expiresIn = 1
284+
}
280285

281286
// used of query mode
282287
params := "access_token=" + authToken.AccessToken.Token + "&token_type=bearer&expires_in=" + strconv.FormatInt(expiresIn, 10) + "&state=" + state + "&id_token=" + authToken.IDToken.Token

server/handlers/oauth_callback.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,12 @@ func OAuthCallbackHandler() gin.HandlerFunc {
150150
if err != nil {
151151
c.JSON(500, gin.H{"error": err.Error()})
152152
}
153-
expiresIn := int64(1800)
153+
154+
expiresIn := authToken.AccessToken.ExpiresAt - time.Now().Unix()
155+
if expiresIn <= 0 {
156+
expiresIn = 1
157+
}
158+
154159
params := "access_token=" + authToken.AccessToken.Token + "&token_type=bearer&expires_in=" + strconv.FormatInt(expiresIn, 10) + "&state=" + stateValue + "&id_token=" + authToken.IDToken.Token
155160

156161
cookie.SetSession(c, authToken.FingerPrintHash)

0 commit comments

Comments
 (0)