Skip to content

Commit

Permalink
refactor(backend): minor refactor to google auth (#1038)
Browse files Browse the repository at this point in the history
- minor refactor in google auth to improve order of execution and logic
  • Loading branch information
detj authored Aug 14, 2024
1 parent 642a605 commit 6722978
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions measure-backend/measure-go/measure/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,14 +412,6 @@ func SigninGoogle(c *gin.Context) {
return
}

// Google API JavaScript client has an open issue where
// it does not send nonce or state in its authorization
// callback
// See: https://github.com/google/google-api-javascript-client/issues/843
//
// If nonce and state, both are empty, we consider it
// valid and proceed for now.

payload, err := idtoken.Validate(ctx, authState.Credential, server.Server.Config.OAuthGoogleKey)
if err != nil {
msg := "failed to validate google credentials"
Expand All @@ -431,18 +423,26 @@ func SigninGoogle(c *gin.Context) {
return
}

checksum, err := cipher.ComputeSHA2Hash([]byte(authState.Nonce))
if err != nil {
fmt.Println(msg, err)
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
"error": msg,
"details": err.Error(),
})
return
}

// Validate nonce if present
//
// Google API JavaScript client has an open issue where
// it does not send nonce or state in its authorization
// callback
// See: https://github.com/google/google-api-javascript-client/issues/843
//
// If nonce and state, both are empty, we consider it
// valid and proceed for now.
if authState.Nonce != "" {
checksum, err := cipher.ComputeSHA2Hash([]byte(authState.Nonce))
if err != nil {
fmt.Println(msg, err)
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
"error": msg,
"details": err.Error(),
})
return
}

if payload.Claims["nonce"] != *checksum {
msg := "failed to validate nonce"
fmt.Println(msg)
Expand Down

0 comments on commit 6722978

Please sign in to comment.