From 89bcd1a0c39239cbe74cbaf561bc51d7a1c4e3da Mon Sep 17 00:00:00 2001 From: Livio Amstutz Date: Mon, 2 Dec 2019 16:15:59 +0100 Subject: [PATCH] error handling --- pkg/op/error.go | 15 ++++++++------- pkg/rp/default_rp.go | 14 +++++++++----- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/pkg/op/error.go b/pkg/op/error.go index 1f7f70a..5c7da30 100644 --- a/pkg/op/error.go +++ b/pkg/op/error.go @@ -2,6 +2,7 @@ package op import ( "net/http" + "net/url" "github.com/caos/oidc/pkg/oidc" "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) return } - url := authReq.GetRedirectURI() + callback := authReq.GetRedirectURI() if authReq.GetResponseType() == oidc.ResponseTypeCode { - url += "?" + callback += "?" } else { - url += "#" + callback += "#" } - url += "error=" + string(e.ErrorType) + callback += "error=" + string(e.ErrorType) if e.Description != "" { - url += "&error_description=" + e.Description + callback += "&error_description=" + url.QueryEscape(e.Description) } 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 { diff --git a/pkg/rp/default_rp.go b/pkg/rp/default_rp.go index 7c956bb..0537443 100644 --- a/pkg/rp/default_rp.go +++ b/pkg/rp/default_rp.go @@ -124,12 +124,16 @@ func (p *DefaultRP) CodeExchangeHandler(callback func(http.ResponseWriter, *http http.Error(w, "failed to get state: "+err.Error(), http.StatusUnauthorized) return } - tokens, err := p.CodeExchange(r.Context(), r.URL.Query().Get("code")) - if err != nil { - http.Error(w, "failed to exchange token: "+err.Error(), http.StatusUnauthorized) - return + params := r.URL.Query() + if params.Get("code") != "" { + tokens, err := p.CodeExchange(r.Context(), params.Get("code")) + 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"))) } }