fix: improve error handling
This commit is contained in:
parent
6cc3c91d07
commit
d2d3395c25
9 changed files with 364 additions and 239 deletions
|
@ -24,7 +24,8 @@ type Exchanger interface {
|
|||
|
||||
func tokenHandler(exchanger Exchanger) func(w http.ResponseWriter, r *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.FormValue("grant_type") {
|
||||
grantType := r.FormValue("grant_type")
|
||||
switch grantType {
|
||||
case string(oidc.GrantTypeCode):
|
||||
CodeExchange(w, r, exchanger)
|
||||
return
|
||||
|
@ -44,10 +45,10 @@ func tokenHandler(exchanger Exchanger) func(w http.ResponseWriter, r *http.Reque
|
|||
return
|
||||
}
|
||||
case "":
|
||||
RequestError(w, r, ErrInvalidRequest("grant_type missing"))
|
||||
RequestError(w, r, oidc.ErrInvalidRequest().WithDescription("grant_type missing"))
|
||||
return
|
||||
}
|
||||
RequestError(w, r, ErrInvalidRequest("grant_type not supported"))
|
||||
RequestError(w, r, oidc.ErrUnsupportedGrantType().WithDescription("%s not supported", grantType))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -63,21 +64,21 @@ type AuthenticatedTokenRequest interface {
|
|||
func ParseAuthenticatedTokenRequest(r *http.Request, decoder utils.Decoder, request AuthenticatedTokenRequest) error {
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
return ErrInvalidRequest("error parsing form")
|
||||
return oidc.ErrInvalidRequest().WithDescription("error parsing form").WithParent(err)
|
||||
}
|
||||
err = decoder.Decode(request, r.Form)
|
||||
if err != nil {
|
||||
return ErrInvalidRequest("error decoding form")
|
||||
return oidc.ErrInvalidRequest().WithDescription("error decoding form").WithParent(err)
|
||||
}
|
||||
clientID, clientSecret, ok := r.BasicAuth()
|
||||
if ok {
|
||||
clientID, err = url.QueryUnescape(clientID)
|
||||
if err != nil {
|
||||
return ErrInvalidRequest("invalid basic auth header")
|
||||
return oidc.ErrInvalidRequest().WithDescription("invalid basic auth header").WithParent(err)
|
||||
}
|
||||
clientSecret, err = url.QueryUnescape(clientSecret)
|
||||
if err != nil {
|
||||
return ErrInvalidRequest("invalid basic auth header")
|
||||
return oidc.ErrInvalidRequest().WithDescription("invalid basic auth header").WithParent(err)
|
||||
}
|
||||
request.SetClientID(clientID)
|
||||
request.SetClientSecret(clientSecret)
|
||||
|
@ -89,7 +90,7 @@ func ParseAuthenticatedTokenRequest(r *http.Request, decoder utils.Decoder, requ
|
|||
func AuthorizeClientIDSecret(ctx context.Context, clientID, clientSecret string, storage Storage) error {
|
||||
err := storage.AuthorizeClientIDSecret(ctx, clientID, clientSecret)
|
||||
if err != nil {
|
||||
return err //TODO: wrap?
|
||||
return oidc.ErrInvalidGrant().WithDescription("code_challenge required").WithParent(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -98,10 +99,10 @@ func AuthorizeClientIDSecret(ctx context.Context, clientID, clientSecret string,
|
|||
//code_challenge of the auth request (PKCE)
|
||||
func AuthorizeCodeChallenge(tokenReq *oidc.AccessTokenRequest, challenge *oidc.CodeChallenge) error {
|
||||
if tokenReq.CodeVerifier == "" {
|
||||
return ErrInvalidRequest("code_challenge required")
|
||||
return oidc.ErrInvalidGrant().WithDescription("code_challenge required") //TODO: ErrInvalidRequest("code_challenge required")
|
||||
}
|
||||
if !oidc.VerifyCodeChallenge(challenge, tokenReq.CodeVerifier) {
|
||||
return ErrInvalidRequest("code_challenge invalid")
|
||||
return oidc.ErrInvalidGrant().WithDescription("invalid code challenge")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -118,7 +119,7 @@ func AuthorizePrivateJWTKey(ctx context.Context, clientAssertion string, exchang
|
|||
return nil, err
|
||||
}
|
||||
if client.AuthMethod() != oidc.AuthMethodPrivateKeyJWT {
|
||||
return nil, ErrInvalidRequest("invalid_client")
|
||||
return nil, oidc.ErrInvalidClient()
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue