error handling

This commit is contained in:
Livio Amstutz 2019-12-02 16:15:59 +01:00
parent d5e1dfff61
commit 89bcd1a0c3
2 changed files with 17 additions and 12 deletions

View file

@ -2,6 +2,7 @@ package op
import ( import (
"net/http" "net/http"
"net/url"
"github.com/caos/oidc/pkg/oidc" "github.com/caos/oidc/pkg/oidc"
"github.com/caos/oidc/pkg/utils" "github.com/caos/oidc/pkg/utils"
@ -92,20 +93,20 @@ func (e *OAuthError) AuthRequestResponse(w http.ResponseWriter, r *http.Request,
http.Error(w, e.Error(), http.StatusBadRequest) http.Error(w, e.Error(), http.StatusBadRequest)
return return
} }
url := authReq.GetRedirectURI() callback := authReq.GetRedirectURI()
if authReq.GetResponseType() == oidc.ResponseTypeCode { if authReq.GetResponseType() == oidc.ResponseTypeCode {
url += "?" callback += "?"
} else { } else {
url += "#" callback += "#"
} }
url += "error=" + string(e.ErrorType) callback += "error=" + string(e.ErrorType)
if e.Description != "" { if e.Description != "" {
url += "&error_description=" + e.Description callback += "&error_description=" + url.QueryEscape(e.Description)
} }
if authReq.GetState() != "" { if authReq.GetState() != "" {
url += "&state=" + authReq.GetState() callback += "&state=" + authReq.GetState()
} }
http.Redirect(w, r, url, http.StatusFound) http.Redirect(w, r, callback, http.StatusFound)
} }
func (e *OAuthError) Error() string { func (e *OAuthError) Error() string {

View file

@ -124,12 +124,16 @@ func (p *DefaultRP) CodeExchangeHandler(callback func(http.ResponseWriter, *http
http.Error(w, "failed to get state: "+err.Error(), http.StatusUnauthorized) http.Error(w, "failed to get state: "+err.Error(), http.StatusUnauthorized)
return return
} }
tokens, err := p.CodeExchange(r.Context(), r.URL.Query().Get("code")) params := r.URL.Query()
if err != nil { if params.Get("code") != "" {
http.Error(w, "failed to exchange token: "+err.Error(), http.StatusUnauthorized) tokens, err := p.CodeExchange(r.Context(), params.Get("code"))
return if err != nil {
http.Error(w, "failed to exchange token: "+err.Error(), http.StatusUnauthorized)
return
}
callback(w, r, tokens, state)
} }
callback(w, r, tokens, state) w.Write([]byte(params.Get("error")))
} }
} }