Skip to content

Commit 6411dc9

Browse files
authored
auth: Avoid forcing one hour expiration for IDP sts creds (#2966)
1 parent 32c34b0 commit 6411dc9

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

pkg/auth/idp/oauth2/config.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ package oauth2
2020

2121
import (
2222
"crypto/sha1"
23+
"strconv"
2324
"strings"
25+
"time"
2426

2527
"github.com/minio/console/pkg/auth/token"
2628
"github.com/minio/pkg/env"
@@ -105,7 +107,14 @@ func getIDPScopes() string {
105107
return env.Get(ConsoleIDPScopes, "openid,profile,email")
106108
}
107109

108-
// getIDPTokenExpiration return default token expiration for access token (in seconds)
109-
func getIDPTokenExpiration() string {
110-
return env.Get(ConsoleIDPTokenExpiration, "3600")
110+
// getIDPTokenExpiration return default token expiration for access token
111+
func getIDPTokenExpiration() time.Duration {
112+
expiration := 12 * 3600
113+
if expStr := env.Get(ConsoleIDPTokenExpiration, ""); expStr != "" {
114+
if exp, err := strconv.Atoi(expStr); err == nil {
115+
expiration = exp
116+
}
117+
}
118+
119+
return time.Duration(expiration) * time.Second
111120
}

pkg/auth/idp/oauth2/provider.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ import (
2525
"fmt"
2626
"net/http"
2727
"net/url"
28-
"strconv"
2928
"strings"
3029
"time"
3130

3231
"github.com/minio/minio-go/v7/pkg/credentials"
3332
"github.com/minio/minio-go/v7/pkg/set"
3433

34+
"github.com/minio/console/pkg/auth/token"
3535
"github.com/minio/console/pkg/auth/utils"
3636
"golang.org/x/crypto/pbkdf2"
3737
"golang.org/x/oauth2"
@@ -331,22 +331,23 @@ func (client *Provider) VerifyIdentity(ctx context.Context, code, state, roleARN
331331
return nil, errors.New("invalid token")
332332
}
333333

334-
// expiration configured in the token itself
335-
expiration := int(oauth2Token.Expiry.Sub(time.Now().UTC()).Seconds())
334+
expiration := token.GetConsoleSTSDuration()
335+
if exp := getIDPTokenExpiration(); exp > 0 {
336+
expiration = exp
337+
}
336338

337-
// check if user configured a hardcoded expiration for console via env variables
338-
// and override the incoming expiration
339-
userConfiguredExpiration := getIDPTokenExpiration()
340-
if userConfiguredExpiration != "" {
341-
expiration, _ = strconv.Atoi(userConfiguredExpiration)
339+
// Use the expiration configured in the token itself if it is closer than the configured value
340+
if exp := oauth2Token.Expiry.Sub(time.Now().UTC()); exp < expiration {
341+
expiration = exp
342342
}
343+
343344
idToken := oauth2Token.Extra("id_token")
344345
if idToken == nil {
345346
return nil, errors.New("missing id_token")
346347
}
347348
token := &credentials.WebIdentityToken{
348349
Token: idToken.(string),
349-
Expiry: expiration,
350+
Expiry: int(expiration.Seconds()),
350351
}
351352
if client.UserInfo { // look for access_token only if userinfo is requested.
352353
accessToken := oauth2Token.Extra("access_token")

pkg/auth/token/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
// GetConsoleSTSDuration returns the default session duration for the STS requested tokens (defaults to 12h)
2727
func GetConsoleSTSDuration() time.Duration {
2828
duration, err := time.ParseDuration(env.Get(ConsoleSTSDuration, "12h"))
29-
if err != nil {
29+
if err != nil || duration <= 0 {
3030
duration = 12 * time.Hour
3131
}
3232
return duration

0 commit comments

Comments
 (0)