From 3d5de74d02486c2300133f40834df71b4213fdaf Mon Sep 17 00:00:00 2001 From: Livio Amstutz Date: Thu, 21 Nov 2019 16:38:51 +0100 Subject: [PATCH] begin parsing --- pkg/oidc/authorization.go | 73 ++++++++++++++++++++++++++++++--------- pkg/op/authrequest.go | 15 +++++++- pkg/op/go.mod | 1 + pkg/utils/formParser.go | 1 + 4 files changed, 73 insertions(+), 17 deletions(-) create mode 100644 pkg/utils/formParser.go diff --git a/pkg/oidc/authorization.go b/pkg/oidc/authorization.go index e26568e..ed75f58 100644 --- a/pkg/oidc/authorization.go +++ b/pkg/oidc/authorization.go @@ -1,6 +1,9 @@ package oidc import ( + "errors" + "strings" + "golang.org/x/text/language" ) @@ -9,10 +12,10 @@ const ( ResponseTypeIDToken = "id_token token" ResponseTypeIDTokenOnly = "id_token" - DisplayPage = "page" - DisplayPopup = "popup" - DisplayTouch = "touch" - DisplayWAP = "wap" + DisplayPage Display = "page" + DisplayPopup Display = "popup" + DisplayTouch Display = "touch" + DisplayWAP Display = "wap" PromptNone = "none" PromptLogin = "login" @@ -20,31 +23,69 @@ const ( PromptSelectAccount = "select_account" ) +var displayValues = map[string]Display{ + "page": DisplayPage, + "popup": DisplayPopup, + "touch": DisplayTouch, + "wap": DisplayWAP, +} + //AuthRequest according to: //https://openid.net/specs/openid-connect-core-1_0.html#AuthRequest // type AuthRequest struct { - Scopes []string `schema:"scope"` + Scopes Scopes `schema:"scope"` ResponseType ResponseType `schema:"response_type"` - ClientID string - RedirectURI string //TODO: type + ClientID string `schema:"client_id"` + RedirectURI string `schema:"redirect_uri"` //TODO: type - State string + State string `schema:"state"` // ResponseMode TODO: ? - Nonce string - Display Display - Prompt Prompt - MaxAge uint32 - UILocales []language.Tag - IDTokenHint string - LoginHint string - ACRValues []string + Nonce string `schema:"nonce"` + Display Display `schema:"display"` + Prompt Prompt `schema:"prompt"` + MaxAge uint32 `schema:"max_age"` + UILocales Locales `schema:"ui_locales"` + IDTokenHint string `schema:"id_token_hint"` + LoginHint string `schema:"login_hint"` + ACRValues []string `schema:"acr_values"` +} + +type Scopes []string + +func (s *Scopes) UnmarshalText(text []byte) error { + scopes := strings.Split(string(text), " ") + *s = Scopes(scopes) + return nil } type ResponseType string type Display string +func (d *Display) UnmarshalText(text []byte) error { + var ok bool + display := string(text) + *d, ok = displayValues[display] + if !ok { + return errors.New("") + } + return nil +} + type Prompt string + +type Locales []language.Tag + +func (l *Locales) UnmarshalText(text []byte) error { + locales := strings.Split(string(text), " ") + for _, locale := range locales { + tag, err := language.Parse(locale) + if err == nil && !tag.IsRoot() { + *l = append(*l, tag) + } + } + return nil +} diff --git a/pkg/op/authrequest.go b/pkg/op/authrequest.go index d631992..9a53238 100644 --- a/pkg/op/authrequest.go +++ b/pkg/op/authrequest.go @@ -4,11 +4,24 @@ import ( "errors" "net/http" + "github.com/gorilla/schema" + "github.com/caos/oidc/pkg/oidc" ) func ParseAuthRequest(w http.ResponseWriter, r *http.Request) (*oidc.AuthRequest, error) { - return nil, errors.New("Unimplemented") //TODO: impl + err := r.ParseForm() + if err != nil { + return nil, errors.New("Unimplemented") //TODO: impl + } + authReq := new(oidc.AuthRequest) + + //TODO: + d := schema.NewDecoder() + d.IgnoreUnknownKeys(true) + + err = d.Decode(authReq, r.Form) + return authReq, err } func ValidateAuthRequest(authRequest *oidc.AuthRequest) error { diff --git a/pkg/op/go.mod b/pkg/op/go.mod index 04596fa..bb6297d 100644 --- a/pkg/op/go.mod +++ b/pkg/op/go.mod @@ -14,5 +14,6 @@ require ( github.com/caos/oidc/pkg/utils v0.0.0-00010101000000-000000000000 github.com/caos/utils/logging v0.0.0-20191104132131-b318678afbef github.com/gorilla/mux v1.7.3 + github.com/gorilla/schema v1.1.0 github.com/stretchr/testify v1.4.0 ) diff --git a/pkg/utils/formParser.go b/pkg/utils/formParser.go new file mode 100644 index 0000000..d4b585b --- /dev/null +++ b/pkg/utils/formParser.go @@ -0,0 +1 @@ +package utils