Skip to content

Commit

Permalink
feat: Add error page on auth callback if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Sep 28, 2022
1 parent 90d6658 commit 6400ffa
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
20 changes: 19 additions & 1 deletion pkg/oidc/authorize_callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package oidc

import (
"context"
"embed"
"html/template"
"net/http"

auth "github.com/formancehq/auth/pkg"
Expand All @@ -11,14 +13,30 @@ import (
"github.com/zitadel/oidc/pkg/op"
)

//go:embed templates
var templateFs embed.FS

func authorizeErrorHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
authError := r.URL.Query().Get("error")
tpl := template.Must(template.New("error.tmpl").
ParseFS(templateFs, "templates/error.tmpl"))
if err := tpl.Execute(w, map[string]interface{}{
"Error": authError,
"ErrorDescription": r.URL.Query().Get("error_description"),
}); err != nil {
panic(err)
}
}
}

func authorizeCallbackHandler(
provider op.OpenIDProvider,
storage Storage,
relyingParty rp.RelyingParty,
) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {

// TODO: error handling
state, err := delegatedauth.DecodeDelegatedState(r.URL.Query().Get("state"))
if err != nil {
panic(err)
Expand Down
23 changes: 23 additions & 0 deletions pkg/oidc/authorize_callback_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package oidc

import (
"io"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/require"
)

func TestAuthorizeError(t *testing.T) {
handler := authorizeErrorHandler()

req := httptest.NewRequest(http.MethodGet, "/?error=foo&error_description=bar", nil)
rec := httptest.NewRecorder()

handler.ServeHTTP(rec, req)

data, err := io.ReadAll(rec.Body)
require.NoError(t, err)
require.Equal(t, string(data), "foo : bar\n")
}
7 changes: 1 addition & 6 deletions pkg/oidc/oidc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,7 @@ func Test3LeggedFlow(t *testing.T) {
if testing.Verbose() {
fmt.Printf("URL:%s\n", authUrl)
}
rsp, err := (&http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
fmt.Println(req.URL.String())
return nil
},
}).Get(authUrl)
rsp, err := http.Get(authUrl)
require.NoError(t, err)
require.Equal(t, http.StatusOK, rsp.StatusCode)

Expand Down
2 changes: 2 additions & 0 deletions pkg/oidc/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ import (
func AddRoutes(router *mux.Router, provider op.OpenIDProvider, storage Storage, relyingParty rp.RelyingParty, baseUrl *url.URL) {
router.NewRoute().Path("/authorize/callback").Queries("code", "{code}").
Handler(authorizeCallbackHandler(provider, storage, relyingParty))
router.NewRoute().Path("/authorize/callback").Queries("error", "{error}").
Handler(authorizeErrorHandler())
router.PathPrefix("/").Handler(http.StripPrefix(baseUrl.Path, provider.HttpHandler()))
}
1 change: 1 addition & 0 deletions pkg/oidc/templates/error.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{.Error}} : {{.ErrorDescription}}

0 comments on commit 6400ffa

Please sign in to comment.