Merge pull request #1187 from trheyi/main
Enhance login process to support team selection and improve token han…
This commit is contained in:
commit
59d0e56982
2 changed files with 62 additions and 42 deletions
|
|
@ -156,34 +156,6 @@ func LoginByUserID(userid string, ip string) (*LoginResponse, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get MFA enabled status from user data
|
|
||||||
mfaEnabled := toBool(user["mfa_enabled"])
|
|
||||||
|
|
||||||
// If MFA enabled, generate MFA token
|
|
||||||
if mfaEnabled {
|
|
||||||
|
|
||||||
// Sign temporary access token for MFA
|
|
||||||
var mfaExpire int = 10 * 60 // 10 minutes
|
|
||||||
accessToken, err := oauth.OAuth.MakeAccessToken(yaoClientConfig.ClientID, ScopeMFAVerification, userid, mfaExpire)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &LoginResponse{
|
|
||||||
UserID: userid,
|
|
||||||
AccessToken: accessToken,
|
|
||||||
ExpiresIn: mfaExpire,
|
|
||||||
MFAEnabled: mfaEnabled,
|
|
||||||
Status: LoginStatusMFA,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update Last Login
|
|
||||||
err = userProvider.UpdateUserLastLogin(ctx, userid, ip)
|
|
||||||
if err != nil {
|
|
||||||
log.Warn("Failed to update last login: %s", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
yaoClientConfig := GetYaoClientConfig()
|
yaoClientConfig := GetYaoClientConfig()
|
||||||
var scopes []string = yaoClientConfig.Scopes
|
var scopes []string = yaoClientConfig.Scopes
|
||||||
if v, ok := user["scopes"].([]string); ok {
|
if v, ok := user["scopes"].([]string); ok {
|
||||||
|
|
@ -194,10 +166,67 @@ func LoginByUserID(userid string, ip string) (*LoginResponse, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn("Failed to store user fingerprint: %s", err.Error())
|
log.Warn("Failed to store user fingerprint: %s", err.Error())
|
||||||
}
|
}
|
||||||
oidcUserInfo := oauthtypes.MakeOIDCUserInfo(user)
|
|
||||||
oidcUserInfo.Sub = subject
|
// Get MFA enabled status from user data
|
||||||
|
mfaEnabled := toBool(user["mfa_enabled"])
|
||||||
|
|
||||||
|
// If MFA enabled, generate MFA token
|
||||||
|
if mfaEnabled {
|
||||||
|
|
||||||
|
// Sign temporary access token for MFA
|
||||||
|
var mfaExpire int = 10 * 60 // 10 minutes
|
||||||
|
accessToken, err := oauth.OAuth.MakeAccessToken(yaoClientConfig.ClientID, ScopeMFAVerification, subject, mfaExpire)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &LoginResponse{
|
||||||
|
UserID: userid,
|
||||||
|
AccessToken: accessToken,
|
||||||
|
ExpiresIn: mfaExpire,
|
||||||
|
MFAEnabled: mfaEnabled,
|
||||||
|
TokenType: "Bearer",
|
||||||
|
Scope: ScopeMFAVerification,
|
||||||
|
Status: LoginStatusMFA,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update Last Login
|
||||||
|
err = userProvider.UpdateUserLastLogin(ctx, userid, ip)
|
||||||
|
if err != nil {
|
||||||
|
log.Warn("Failed to update last login: %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Count User Teams
|
||||||
|
numTeams, err := getUserTeamsCount(ctx, userid)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// If user has teams, return team selection status with temporary access token
|
||||||
|
if numTeams > 0 {
|
||||||
|
// Sign temporary access token for Team Selection
|
||||||
|
var teamSelectionExpire int = 10 * 60 // 10 minutes
|
||||||
|
accessToken, err := oauth.OAuth.MakeAccessToken(yaoClientConfig.ClientID, ScopeTeamSelection, subject, teamSelectionExpire)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &LoginResponse{
|
||||||
|
UserID: userid,
|
||||||
|
Subject: subject,
|
||||||
|
AccessToken: accessToken,
|
||||||
|
ExpiresIn: teamSelectionExpire,
|
||||||
|
MFAEnabled: mfaEnabled,
|
||||||
|
TokenType: "Bearer",
|
||||||
|
Scope: ScopeTeamSelection,
|
||||||
|
Status: LoginStatusTeamSelection,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
// OIDC Token
|
// OIDC Token
|
||||||
|
oidcUserInfo := oauthtypes.MakeOIDCUserInfo(user)
|
||||||
|
oidcUserInfo.Sub = subject
|
||||||
oidcToken, err := oauth.OAuth.SignIDToken(yaoClientConfig.ClientID, strings.Join(scopes, " "), yaoClientConfig.ExpiresIn, oidcUserInfo)
|
oidcToken, err := oauth.OAuth.SignIDToken(yaoClientConfig.ClientID, strings.Join(scopes, " "), yaoClientConfig.ExpiresIn, oidcUserInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -215,17 +244,6 @@ func LoginByUserID(userid string, ip string) (*LoginResponse, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Count User Teams
|
|
||||||
numTeams, err := getUserTeamsCount(ctx, userid)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
status := LoginStatusSuccess
|
|
||||||
if numTeams > 0 {
|
|
||||||
status = LoginStatusTeamSelection
|
|
||||||
}
|
|
||||||
|
|
||||||
return &LoginResponse{
|
return &LoginResponse{
|
||||||
UserID: userid,
|
UserID: userid,
|
||||||
Subject: subject,
|
Subject: subject,
|
||||||
|
|
@ -237,7 +255,7 @@ func LoginByUserID(userid string, ip string) (*LoginResponse, error) {
|
||||||
TokenType: "Bearer",
|
TokenType: "Bearer",
|
||||||
MFAEnabled: mfaEnabled,
|
MFAEnabled: mfaEnabled,
|
||||||
Scope: strings.Join(scopes, " "),
|
Scope: strings.Join(scopes, " "),
|
||||||
Status: status,
|
Status: LoginStatusSuccess,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,8 @@ const (
|
||||||
const (
|
const (
|
||||||
// ScopeMFAVerification is the MFA verification scope for temporary access token
|
// ScopeMFAVerification is the MFA verification scope for temporary access token
|
||||||
ScopeMFAVerification = "mfa_verification"
|
ScopeMFAVerification = "mfa_verification"
|
||||||
|
// ScopeTeamSelection is the team selection scope for temporary access token
|
||||||
|
ScopeTeamSelection = "team_selection"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config represents the signin page configuration
|
// Config represents the signin page configuration
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue