Skip to content

Commit

Permalink
Merge pull request #11 from MWM-io/dr/add-is-eligible-token
Browse files Browse the repository at this point in the history
[middleware] Add IsEligible func to verify if it is an GCloudSA token
  • Loading branch information
Dmouri committed May 2, 2024
2 parents 5b5ee0b + 64cbcb5 commit b2bda19
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions gcloud/middleware/auth/gcloud_service_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,35 @@ func (m GCloudServiceAccount) VerifyServiceAccount(r *http.Request, token string
return nil
}

// IsEligible checks if the request is eligible for the middleware
func (m GCloudServiceAccount) IsEligible(r *http.Request) bool {
token := r.Header.Get(AuthorizationHeader)
if token == "" {
return false
}

splitAuthHeader := strings.Split(token, " ")
if len(splitAuthHeader) == 0 {
return false
}

if len(splitAuthHeader) > 1 {
payload, err := idtoken.Validate(r.Context(), token, "")
if err != nil {
// invalid token
return false
}

if payload.Issuer != "accounts.google.com" && payload.Issuer != "https://accounts.google.com" {
return false
}

return true
}

return false
}

const securitySchemeKey = "gcloud_service_account"

// Doc implements the openapi.Documented interface
Expand Down

0 comments on commit b2bda19

Please sign in to comment.