-
Notifications
You must be signed in to change notification settings - Fork 345
Expand file tree
/
Copy pathflow_none_auth.go
More file actions
64 lines (51 loc) · 2 KB
/
Copy pathflow_none_auth.go
File metadata and controls
64 lines (51 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package oauth2
import (
"context"
"net/url"
"strings"
"github.com/ory/x/errorsx"
"github.com/ory/fosite"
)
var _ fosite.AuthorizeEndpointHandler = (*NoneResponseTypeHandler)(nil)
// NoneResponseTypeHandler is a response handler for when the None response type is requested
// as defined in https://openid.net/specs/oauth-v2-multiple-response-types-1_0.html#none
type NoneResponseTypeHandler struct {
Config interface {
fosite.ScopeStrategyProvider
fosite.AudienceStrategyProvider
fosite.RedirectSecureCheckerProvider
fosite.OmitRedirectScopeParamProvider
}
}
func (c *NoneResponseTypeHandler) secureChecker(ctx context.Context) func(context.Context, *url.URL) bool {
if c.Config.GetRedirectSecureChecker(ctx) == nil {
return fosite.IsRedirectURISecure
}
return c.Config.GetRedirectSecureChecker(ctx)
}
func (c *NoneResponseTypeHandler) HandleAuthorizeEndpointRequest(ctx context.Context, ar fosite.AuthorizeRequester, resp fosite.AuthorizeResponder) error {
if !ar.GetResponseTypes().ExactOne("none") {
return nil
}
ar.SetDefaultResponseMode(fosite.ResponseModeQuery)
if !c.secureChecker(ctx)(ctx, 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.Config.GetScopeStrategy(ctx)(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.Config.GetAudienceStrategy(ctx)(client.GetAudience(), ar.GetRequestedAudience()); err != nil {
return err
}
resp.AddParameter("state", ar.GetState())
if !c.Config.GetOmitRedirectScopeParam(ctx) {
resp.AddParameter("scope", strings.Join(ar.GetGrantedScopes(), " "))
}
ar.SetResponseTypeHandled("none")
return nil
}