Skip to content

Commit

Permalink
make approval url dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
256dpi committed Nov 25, 2019
1 parent 2c9f835 commit 40ef7e4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func createHandler(store *coal.Store) http.Handler {
policy.ClientCredentialsGrant = true
policy.ImplicitGrant = true
policy.AuthorizationCodeGrant = true
policy.ApprovalURL = "http://0.0.0.0:4200/authorize"
policy.ApprovalURL = flame.StaticApprovalURL("http://0.0.0.0:4200/authorize")
policy.GrantStrategy = func(scope oauth2.Scope, client flame.Client, owner flame.ResourceOwner) (oauth2.Scope, error) {
return scope, nil
}
Expand Down
9 changes: 6 additions & 3 deletions flame/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,11 @@ func (a *Authenticator) authorizationEndpoint(env *environment) {

// check request method
if env.request.Method == "GET" {
// abort if approval URL is not configured
if a.policy.ApprovalURL == "" {
// get approval url
url, err := a.policy.ApprovalURL(client)
if err != nil {
stack.Abort(err)
} else if url == "" {
abort(oauth2.InvalidRequest("unsupported request method"))
}

Expand All @@ -320,7 +323,7 @@ func (a *Authenticator) authorizationEndpoint(env *environment) {
}

// perform redirect
stack.AbortIf(oauth2.WriteRedirect(env.writer, a.policy.ApprovalURL, params, false))
stack.AbortIf(oauth2.WriteRedirect(env.writer, url, params, false))

return
}
Expand Down
16 changes: 12 additions & 4 deletions flame/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ type Policy struct {
ImplicitGrant bool
AuthorizationCodeGrant bool

// The URL to the page that obtains the approval of the user in implicit and
// authorization code grants.
ApprovalURL string

// The token model.
Token GenericToken

Expand Down Expand Up @@ -75,6 +71,10 @@ type Policy struct {
// Note: ResourceOwner is not set for a client credentials grant.
GrantStrategy func(oauth2.Scope, Client, ResourceOwner) (oauth2.Scope, error)

// The URL to the page that obtains the approval of the user in implicit and
// authorization code grants.
ApprovalURL func(Client) (string, error)

// ApproveStrategy is invoked by the authenticator to verify the
// authorization approval by an authenticated resource owner in the implicit
// grant and authorization code grant flows. The callback should return the
Expand Down Expand Up @@ -105,6 +105,13 @@ func DefaultGrantStrategy(scope oauth2.Scope, _ Client, _ ResourceOwner) (oauth2
return scope, nil
}

// StaticApprovalURL returns a static approval URL.
func StaticApprovalURL(url string) func(Client) (string, error) {
return func(Client) (string, error) {
return url, nil
}
}

// DefaultApproveStrategy rejects all approvals.
func DefaultApproveStrategy(GenericToken, oauth2.Scope, Client, ResourceOwner) (oauth2.Scope, error) {
return nil, ErrApprovalRejected
Expand Down Expand Up @@ -132,6 +139,7 @@ func DefaultPolicy(secret string) *Policy {
return []ResourceOwner{&User{}}
},
GrantStrategy: DefaultGrantStrategy,
ApprovalURL: StaticApprovalURL(""),
ApproveStrategy: DefaultApproveStrategy,
TokenData: DefaultTokenData,
AccessTokenLifespan: time.Hour,
Expand Down

0 comments on commit 40ef7e4

Please sign in to comment.