Skip to content

Commit

Permalink
godoc
Browse files Browse the repository at this point in the history
  • Loading branch information
reinkrul committed Aug 19, 2023
1 parent 192a2d0 commit f1ee98b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 49 deletions.
2 changes: 1 addition & 1 deletion vcr/api/oauth2/v0/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (r Wrapper) HandleAuthorizeRequest(ctx context.Context, request HandleAutho
return nil, err
}
if result != nil {
return HandleAuthorizeRequest200TexthtmlResponse{Body: bytes.NewReader(result.HTML), ContentLength: int64(len(result.HTML))}, nil
return HandleAuthorizeRequest200TexthtmlResponse{Body: bytes.NewReader(result.html), ContentLength: int64(len(result.html))}, nil
}
}

Expand Down
2 changes: 1 addition & 1 deletion vcr/api/oauth2/v0/authorized_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (a authorizedCodeFlow) handleAuthzRequest(params map[string]string, session
return nil, fmt.Errorf("unable to render authorization page: %w", err)
}
return &authzResponse{
HTML: buf.Bytes(),
html: buf.Bytes(),
}, nil
}

Expand Down
51 changes: 5 additions & 46 deletions vcr/api/oauth2/v0/interface.go
Original file line number Diff line number Diff line change
@@ -1,64 +1,23 @@
package v0

import (
"github.com/google/uuid"
"github.com/nuts-foundation/nuts-node/core"
"net/url"
"sync"
)

// authzResponse is the response to an Authorization Code flow request.
type authzResponse struct {
HTML []byte
// html is the HTML page to be rendered to the user.
html []byte
}

type protocol interface {
core.Routable
// handleAuthzRequest handles an Authorization Code flow request and returns an authzResponse if the request is handled by this protocol.
// If the protocol can't handle the supplied parameters it returns nil.
handleAuthzRequest(map[string]string, *Session) (*authzResponse, error)
grantHandlers() map[string]grantHandler
}

// authzHandler defines a function for checking authorization requests given the input parameters, used to initiate the authorization code flow.
type authzHandler func(map[string]string, *Session) (bool, error)

// grantHandler defines a function for checking a grant given the input parameters, used to validate token requests.
// It returns the requested scopes if the validation succeeds.
type grantHandler func(map[string]string) (string, error)

type SessionManager struct {
sessions *sync.Map
}

func (s *SessionManager) Create(session Session) string {
// TODO: Session expiration
// TODO: Session storage
// TODO: Session pinning and other safety measures (see OAuth2 Threat Model)
id := uuid.NewString()
s.sessions.Store(id, session)
return id
}

func (s *SessionManager) Get(id string) *Session {
session, ok := s.sessions.Load(id)
if !ok {
return nil
}
result := session.(Session)
return &result
}

type Session struct {
ClientID string
Scope string
ClientState string
RedirectURI string
}

func (s Session) CreateRedirectURI(params map[string]string) string {
redirectURI, _ := url.Parse(s.RedirectURI)
query := redirectURI.Query()
for key, value := range params {
query.Add(key, value)
}
redirectURI.RawQuery = query.Encode()
return redirectURI.String()
}
2 changes: 1 addition & 1 deletion vcr/api/oauth2/v0/openid4vp.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (a openID4VP) handleAuthzRequest(params map[string]string, session *Session
return nil, fmt.Errorf("unable to render authorization page: %w", err)
}
return &authzResponse{
HTML: buf.Bytes(),
html: buf.Bytes(),
}, nil
}

Expand Down
46 changes: 46 additions & 0 deletions vcr/api/oauth2/v0/session.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package v0

import (
"github.com/google/uuid"
"net/url"
"sync"
)

type SessionManager struct {
sessions *sync.Map
}

func (s *SessionManager) Create(session Session) string {
// TODO: Session expiration
// TODO: Session storage
// TODO: Session pinning and other safety measures (see OAuth2 Threat Model)
id := uuid.NewString()
s.sessions.Store(id, session)
return id
}

func (s *SessionManager) Get(id string) *Session {
session, ok := s.sessions.Load(id)
if !ok {
return nil
}
result := session.(Session)
return &result
}

type Session struct {
ClientID string
Scope string
ClientState string
RedirectURI string
}

func (s Session) CreateRedirectURI(params map[string]string) string {
redirectURI, _ := url.Parse(s.RedirectURI)
query := redirectURI.Query()
for key, value := range params {
query.Add(key, value)
}
redirectURI.RawQuery = query.Encode()
return redirectURI.String()
}

0 comments on commit f1ee98b

Please sign in to comment.