forked from ory/fosite
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd2406a
commit f9e075b
Showing
18 changed files
with
1,432 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package compose | ||
|
||
import ( | ||
"github.com/ory/fosite/handler/par" | ||
) | ||
|
||
// PushedAuthorizeHandlerFactory creates the basic PAR handler | ||
func PushedAuthorizeHandlerFactory(config *Config, storage interface{}, strategy interface{}) interface{} { | ||
return &par.PushedAuthorizeHandler{ | ||
Storage: storage, | ||
RequestURIPrefix: config.PushedAuthorizationRequestURIPrefix, | ||
PARContextLifetime: config.PushedAuthorizationContextLifespan, | ||
ScopeStrategy: config.GetScopeStrategy(), | ||
AudienceMatchingStrategy: config.GetAudienceStrategy(), | ||
IsRedirectURISecure: config.GetRedirectSecureChecker(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package par | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"fmt" | ||
"net/url" | ||
"time" | ||
|
||
"github.com/ory/fosite" | ||
"github.com/ory/fosite/token/hmac" | ||
"github.com/ory/x/errorsx" | ||
) | ||
|
||
const ( | ||
defaultPARKeyLength = 32 | ||
) | ||
|
||
var b64 = base64.URLEncoding.WithPadding(base64.NoPadding) | ||
|
||
// PushedAuthorizeHandler handles the PAR request | ||
type PushedAuthorizeHandler struct { | ||
Storage interface{} | ||
PARContextLifetime time.Duration | ||
RequestURIPrefix string | ||
ScopeStrategy fosite.ScopeStrategy | ||
AudienceMatchingStrategy fosite.AudienceMatchingStrategy | ||
|
||
IsRedirectURISecure func(*url.URL) bool | ||
} | ||
|
||
// HandlePushedAuthorizeEndpointRequest handles a pushed authorize endpoint request. To extend the handler's capabilities, the http request | ||
// is passed along, if further information retrieval is required. If the handler feels that he is not responsible for | ||
// the pushed authorize request, he must return nil and NOT modify session nor responder neither requester. | ||
func (c *PushedAuthorizeHandler) HandlePushedAuthorizeEndpointRequest(ctx context.Context, ar fosite.AuthorizeRequester, resp fosite.PushedAuthorizeResponder) error { | ||
storage, ok := c.Storage.(fosite.PARStorage) | ||
if !ok { | ||
return errorsx.WithStack(fosite.ErrServerError.WithHint("Invalid storage type")) | ||
} | ||
|
||
if !ar.GetResponseTypes().HasOneOf("token", "code", "id_token") { | ||
return nil | ||
} | ||
|
||
if !c.secureChecker()(ar.GetRedirectURI()) { | ||
return errorsx.WithStack(fosite.ErrInvalidRequest.WithHint("Redirect URL is using an insecure protocol, http is only allowed for hosts with suffix `localhost`, for example: http://myapp.localhost/.")) | ||
} | ||
|
||
client := ar.GetClient() | ||
for _, scope := range ar.GetRequestedScopes() { | ||
if !c.ScopeStrategy(client.GetScopes(), scope) { | ||
return errorsx.WithStack(fosite.ErrInvalidScope.WithHintf("The OAuth 2.0 Client is not allowed to request scope '%s'.", scope)) | ||
} | ||
} | ||
|
||
if err := c.AudienceMatchingStrategy(client.GetAudience(), ar.GetRequestedAudience()); err != nil { | ||
return err | ||
} | ||
|
||
expiresIn := c.PARContextLifetime | ||
if ar.GetSession() != nil { | ||
ar.GetSession().SetExpiresAt(fosite.PushedAuthorizeRequestContext, time.Now().UTC().Add(expiresIn)) | ||
} | ||
|
||
// generate an ID | ||
stateKey, err := hmac.RandomBytes(defaultPARKeyLength) | ||
if err != nil { | ||
return errorsx.WithStack(fosite.ErrInsufficientEntropy.WithHint("Unable to generate the random part of the request_uri.").WithWrap(err).WithDebug(err.Error())) | ||
} | ||
|
||
requestURI := fmt.Sprintf("%s%s", c.RequestURIPrefix, b64.EncodeToString(stateKey)) | ||
|
||
// store | ||
if err = storage.CreatePARSession(ctx, requestURI, ar); err != nil { | ||
return errorsx.WithStack(fosite.ErrServerError.WithHint("Unable to store the PAR session").WithWrap(err).WithDebug(err.Error())) | ||
} | ||
|
||
resp.SetRequestURI(requestURI) | ||
resp.SetExpiresIn(int(expiresIn.Seconds())) | ||
return nil | ||
} | ||
|
||
func (c *PushedAuthorizeHandler) secureChecker() func(*url.URL) bool { | ||
if c.IsRedirectURISecure == nil { | ||
c.IsRedirectURISecure = fosite.IsRedirectURISecure | ||
} | ||
return c.IsRedirectURISecure | ||
} |
Oops, something went wrong.