forked from ory/fosite
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdevice_request_handler.go
More file actions
68 lines (56 loc) · 2.33 KB
/
Copy pathdevice_request_handler.go
File metadata and controls
68 lines (56 loc) · 2.33 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
65
66
67
68
// Copyright © 2026 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package fosite
import (
"context"
"net/http"
"strings"
"github.com/ory/x/errorsx"
"github.com/ory/x/otelx"
"go.opentelemetry.io/otel/trace"
"github.com/ory/fosite/i18n"
)
// NewDeviceRequest parses an http Request returns a Device request
func (f *Fosite) NewDeviceRequest(ctx context.Context, r *http.Request) (_ DeviceRequester, err error) {
ctx, span := trace.SpanFromContext(ctx).TracerProvider().Tracer("github.com/ory/fosite").Start(ctx, "Fosite.NewDeviceRequest")
defer otelx.End(span, &err)
request := NewDeviceRequest()
request.Lang = i18n.GetLangFromRequest(f.Config.GetMessageCatalog(ctx), r)
if r.Method != http.MethodPost {
return request, errorsx.WithStack(ErrInvalidRequest.WithHintf("HTTP method is '%s', expected 'POST'.", r.Method))
}
if err := r.ParseForm(); err != nil {
return request, errorsx.WithStack(ErrInvalidRequest.WithHint("Unable to parse HTTP body, make sure to send a properly formatted form request body.").WithWrap(err).WithDebug(err.Error()))
}
if len(r.PostForm) == 0 {
return request, errorsx.WithStack(ErrInvalidRequest.WithHint("The POST body can not be empty."))
}
request.Form = r.PostForm
if _, err := ValidateResourceIndicator(request.GetRequestForm()); err != nil {
return request, err
}
client, clientErr := f.AuthenticateClient(ctx, r, r.PostForm)
if clientErr != nil {
return request, clientErr
}
if client.GetID() != request.Form.Get("client_id") {
return request, errorsx.WithStack(ErrInvalidRequest.WithHint("Provided client_id mismatch."))
}
request.Client = client
if !client.GetGrantTypes().Has(string(GrantTypeDeviceCode)) {
return request, errorsx.WithStack(ErrInvalidGrant.WithHint("The requested OAuth 2.0 Client does not have the 'urn:ietf:params:oauth:grant-type:device_code' grant."))
}
if err := f.validateDeviceScope(ctx, r, request); err != nil {
return request, err
}
return request, nil
}
func (f *Fosite) validateDeviceScope(ctx context.Context, r *http.Request, request *DeviceRequest) error {
scopes := Arguments(RemoveEmpty(strings.Split(request.Form.Get("scope"), " ")))
scopes, err := FilterRequestedScopes(f.Config.GetScopeStrategy(ctx), request.Client, scopes, f.Config.GetIgnoreUnknownScopes(ctx))
if err != nil {
return err
}
request.SetRequestedScopes(scopes)
return nil
}