Skip to content

Commit

Permalink
Fix: Simplify logging and cleanup conditional logic
Browse files Browse the repository at this point in the history
Removed unused log keys and consolidated conditional logic in various functions. The code now utilizes switch statements for clarity and refactored the handling of claim types to reduce redundancy. Enhanced processAuthOkLogin with additional logic for Lua filters and post-actions.

Signed-off-by: Christian Roessner <c@roessner.co>
  • Loading branch information
Christian Roessner committed Oct 28, 2024
1 parent 6004260 commit 564e628
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 132 deletions.
49 changes: 24 additions & 25 deletions server/core/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -1411,9 +1411,14 @@ func (a *AuthState) handleLocalCache(ctx *gin.Context) global.AuthResult {
a.setOperationMode(ctx)

passDBResult := a.initializePassDBResult()
authResult := a.filterLua(passDBResult, ctx)

a.postLuaAction(passDBResult)
authResult := global.AuthResultOK

if !(a.Protocol.Get() == global.ProtoOryHydra) {
authResult = a.filterLua(passDBResult, ctx)

a.postLuaAction(passDBResult)
}

return authResult
}
Expand Down Expand Up @@ -1607,9 +1612,13 @@ func (a *AuthState) postVerificationProcesses(ctx *gin.Context, useCache bool, b
}
}

authResult := a.filterLua(passDBResult, ctx)
authResult := global.AuthResultOK

a.postLuaAction(passDBResult)
if !(a.Protocol.Get() == global.ProtoOryHydra) {
authResult = a.filterLua(passDBResult, ctx)

a.postLuaAction(passDBResult)
}

return authResult
}
Expand Down Expand Up @@ -2526,7 +2535,6 @@ func (a *AuthState) processCustomClaims(scopeIndex int, oauth2Client openapi.OAu
for claimIndex := range customScope.Claims {
customClaimName := customScope.Claims[claimIndex].Name
customClaimType := customScope.Claims[claimIndex].Type
valueTypeMatch := false

for clientIndex := range config.LoadableConfig.Oauth2.Clients {
if config.LoadableConfig.Oauth2.Clients[clientIndex].ClientId != oauth2Client.GetClientId() {
Expand All @@ -2548,54 +2556,45 @@ func (a *AuthState) processCustomClaims(scopeIndex int, oauth2Client openapi.OAu
"value", fmt.Sprintf("%#v", value),
)

if customClaimType == global.ClaimTypeString {
switch customClaimType {
case global.ClaimTypeString:
if arg, assertOk := value[global.SliceWithOneElement].(string); assertOk {
claims[customClaimName] = arg
valueTypeMatch = true
}
} else if customClaimType == global.ClaimTypeFloat {
case global.ClaimTypeFloat:
if arg, assertOk := value[global.SliceWithOneElement].(float64); assertOk {
claims[customClaimName] = arg
valueTypeMatch = true
} else if arg, assertOk := value[global.SliceWithOneElement].(string); assertOk {
if number, err := strconv.ParseFloat(arg, 64); err == nil {
claims[customClaimName] = number
valueTypeMatch = true
}
}
} else if customClaimType == global.ClaimTypeInteger {
case global.ClaimTypeInteger:
if arg, assertOk := value[global.SliceWithOneElement].(int64); assertOk {
claims[customClaimName] = arg
valueTypeMatch = true
} else if arg, assertOk := value[global.SliceWithOneElement].(string); assertOk {
if number, err := strconv.ParseInt(arg, 0, 64); err == nil {
claims[customClaimName] = number
valueTypeMatch = true
}
}
} else if customClaimType == global.ClaimTypeBoolean {
case global.ClaimTypeBoolean:
if arg, assertOk := value[global.SliceWithOneElement].(bool); assertOk {
claims[customClaimName] = arg
valueTypeMatch = true
} else if arg, assertOk := value[global.SliceWithOneElement].(string); assertOk {
if boolean, err := strconv.ParseBool(arg); err == nil {
claims[customClaimName] = boolean
valueTypeMatch = true
}
}
default:
level.Error(log.Logger).Log(
global.LogKeyGUID, a.GUID,
"custom_claim_name", customClaimName,
global.LogKeyError, fmt.Sprintf("Unknown type '%s'", customClaimType),
)
}
}
}

if !valueTypeMatch {
level.Error(log.Logger).Log(
global.LogKeyGUID, a.GUID,
"custom_claim_name", customClaimName,
global.LogKeyError, fmt.Sprintf("Unknown type '%s'", customClaimType),
)

}

break
}
}
Expand Down
Loading

0 comments on commit 564e628

Please sign in to comment.