...
1
21
22 package fosite
23
24 import (
25 "context"
26 "net/http"
27 "strings"
28
29 "github.com/ory/x/errorsx"
30
31 "github.com/pkg/errors"
32 )
33
34 type TokenIntrospector interface {
35 IntrospectToken(ctx context.Context, token string, tokenUse TokenUse, accessRequest AccessRequester, scopes []string) (TokenUse, error)
36 }
37
38 func AccessTokenFromRequest(req *http.Request) string {
39
40
41
42
43 auth := req.Header.Get("Authorization")
44 split := strings.SplitN(auth, " ", 2)
45 if len(split) != 2 || !strings.EqualFold(split[0], "bearer") {
46
47
48 if err := req.ParseMultipartForm(1 << 20); err != nil && err != http.ErrNotMultipart {
49 return ""
50 }
51 return req.Form.Get("access_token")
52 }
53
54 return split[1]
55 }
56
57 func (f *Fosite) IntrospectToken(ctx context.Context, token string, tokenUse TokenUse, session Session, scopes ...string) (TokenUse, AccessRequester, error) {
58 var found = false
59 var foundTokenUse TokenUse = ""
60
61 ar := NewAccessRequest(session)
62 for _, validator := range f.TokenIntrospectionHandlers {
63 tu, err := validator.IntrospectToken(ctx, token, tokenUse, ar, scopes)
64 if err == nil {
65 found = true
66 foundTokenUse = tu
67 } else if errors.Is(err, ErrUnknownRequest) {
68
69 } else {
70 rfcerr := ErrorToRFC6749Error(err)
71 return "", nil, errorsx.WithStack(rfcerr)
72 }
73 }
74
75 if !found {
76 return "", nil, errorsx.WithStack(ErrRequestUnauthorized.WithHint("Unable to find a suitable validation strategy for the token, thus it is invalid."))
77 }
78
79 return foundTokenUse, ar, nil
80 }
81
View as plain text