Skip to content

Commit 1535eec

Browse files
authored
feat: max_age is set during auth request to limit user session (#1134)
relates to STACKITCLI-294
1 parent ea693ee commit 1535eec

File tree

8 files changed

+23
-10
lines changed

8 files changed

+23
-10
lines changed

docs/stackit_config_set.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ stackit config set [flags]
5555
--serverbackup-custom-endpoint string Server Backup API base URL, used in calls to this API
5656
--service-account-custom-endpoint string Service Account API base URL, used in calls to this API
5757
--service-enablement-custom-endpoint string Service Enablement API base URL, used in calls to this API
58-
--session-time-limit string Maximum time before authentication is required again. After this time, you will be prompted to login again to execute commands that require authentication. Can't be larger than 24h. Requires authentication after being set to take effect. Examples: 3h, 5h30m40s (BETA: currently values greater than 2h have no effect)
58+
--session-time-limit string Maximum time before authentication is required again. After this time, you will be prompted to login again to execute commands that require authentication. Can't be larger than 24h. Requires authentication after being set to take effect. Examples: 3h, 5h30m40s
5959
--ske-custom-endpoint string SKE API base URL, used in calls to this API
6060
--sqlserverflex-custom-endpoint string SQLServer Flex API base URL, used in calls to this API
6161
--token-custom-endpoint string Custom token endpoint of the Service Account API, which is used to request access tokens when the service account authentication is activated. Not relevant for user authentication.

docs/stackit_config_unset.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ stackit config unset [flags]
5656
--serverbackup-custom-endpoint Server Backup base URL. If unset, uses the default base URL
5757
--service-account-custom-endpoint Service Account API base URL. If unset, uses the default base URL
5858
--service-enablement-custom-endpoint Service Enablement API base URL. If unset, uses the default base URL
59-
--session-time-limit Maximum time before authentication is required again. If unset, defaults to 2h
59+
--session-time-limit Maximum time before authentication is required again. If unset, defaults to 12h
6060
--ske-custom-endpoint SKE API base URL. If unset, uses the default base URL
6161
--sqlserverflex-custom-endpoint SQLServer Flex API base URL. If unset, uses the default base URL
6262
--token-custom-endpoint Custom token endpoint of the Service Account API, which is used to request access tokens when the service account authentication is activated. Not relevant for user authentication.

internal/cmd/config/profile/import/template/profile.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"serverbackup_custom_endpoint": "",
2626
"service_account_custom_endpoint": "",
2727
"service_enablement_custom_endpoint": "",
28-
"session_time_limit": "2h",
28+
"session_time_limit": "12h",
2929
"ske_custom_endpoint": "",
3030
"sqlserverflex_custom_endpoint": "",
3131
"token_custom_endpoint": "",

internal/cmd/config/set/set.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ Use "{{.CommandPath}} [command] --help" for more information about a command.{{e
135135
}
136136

137137
func configureFlags(cmd *cobra.Command) {
138-
cmd.Flags().String(sessionTimeLimitFlag, "", "Maximum time before authentication is required again. After this time, you will be prompted to login again to execute commands that require authentication. Can't be larger than 24h. Requires authentication after being set to take effect. Examples: 3h, 5h30m40s (BETA: currently values greater than 2h have no effect)")
138+
cmd.Flags().String(sessionTimeLimitFlag, "", "Maximum time before authentication is required again. After this time, you will be prompted to login again to execute commands that require authentication. Can't be larger than 24h. Requires authentication after being set to take effect. Examples: 3h, 5h30m40s")
139139
cmd.Flags().String(identityProviderCustomWellKnownConfigurationFlag, "", "Identity Provider well-known OpenID configuration URL, used for user authentication")
140140
cmd.Flags().String(identityProviderCustomClientIdFlag, "", "Identity Provider client ID, used for user authentication")
141141
cmd.Flags().String(allowedUrlDomainFlag, "", `Domain name, used for the verification of the URLs that are given in the custom identity provider endpoint and "STACKIT curl" command`)

internal/pkg/auth/auth.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,23 @@ func GetAccessToken() (string, error) {
110110

111111
func getStartingSessionExpiresAtUnix() (string, error) {
112112
sessionStart := time.Now()
113-
sessionTimeLimitString := viper.GetString(config.SessionTimeLimitKey)
114-
sessionTimeLimit, err := time.ParseDuration(sessionTimeLimitString)
113+
sessionTimeLimit, err := getSessionExpiration()
115114
if err != nil {
116-
return "", fmt.Errorf("parse session time limit \"%s\": %w", sessionTimeLimitString, err)
115+
return "", err
117116
}
118117
sessionExpiresAt := sessionStart.Add(sessionTimeLimit)
119118
return strconv.FormatInt(sessionExpiresAt.Unix(), 10), nil
120119
}
121120

121+
func getSessionExpiration() (time.Duration, error) {
122+
sessionTimeLimitString := viper.GetString(config.SessionTimeLimitKey)
123+
duration, err := time.ParseDuration(sessionTimeLimitString)
124+
if err != nil {
125+
return 0, fmt.Errorf("parse session time limit \"%s\": %w", sessionTimeLimitString, err)
126+
}
127+
return duration, nil
128+
}
129+
122130
func getEmailFromToken(token string) (string, error) {
123131
// We can safely use ParseUnverified because we are not authenticating the user at this point,
124132
// We are parsing the token just to get the service account e-mail

internal/pkg/auth/user_login.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,13 @@ func AuthorizeUser(p *print.Printer, isReauthentication bool) error {
121121
// Initialize the code verifier
122122
codeVerifier := oauth2.GenerateVerifier()
123123

124+
// Generate max age based on the session time limit
125+
maxSessionDuration, err := getSessionExpiration()
126+
if err != nil {
127+
return err
128+
}
124129
// Construct the authorization URL
125-
authorizationURL := conf.AuthCodeURL("", oauth2.S256ChallengeOption(codeVerifier))
130+
authorizationURL := conf.AuthCodeURL("", oauth2.S256ChallengeOption(codeVerifier), oauth2.SetAuthURLParam("max_age", fmt.Sprintf("%d", int64(maxSessionDuration.Seconds()))))
126131

127132
// Start a web server to listen on a callback URL
128133
mux := http.NewServeMux()

internal/pkg/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const (
5555

5656
AsyncDefault = false
5757
RegionDefault = "eu01"
58-
SessionTimeLimitDefault = "2h"
58+
SessionTimeLimitDefault = "12h"
5959

6060
AllowedUrlDomainDefault = "stackit.cloud"
6161
)

internal/pkg/config/template/test_profile.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"serverbackup_custom_endpoint": "",
2626
"service_account_custom_endpoint": "",
2727
"service_enablement_custom_endpoint": "",
28-
"session_time_limit": "2h",
28+
"session_time_limit": "12h",
2929
"ske_custom_endpoint": "",
3030
"sqlserverflex_custom_endpoint": "",
3131
"token_custom_endpoint": "",

0 commit comments

Comments
 (0)