88 "strings"
99 "time"
1010
11- "github.com/mohae/deepcopy"
12-
1311 "github.com/ory/x/errorsx"
1412
1513 "github.com/ory/x/randx"
@@ -20,83 +18,6 @@ import (
2018
2119const POLLING_RATE_LIMITING_LEEWAY = 200 * time .Millisecond
2220
23- // DeviceFlowSession is a fosite.Session container specific for the device flow.
24- type DeviceFlowSession interface {
25- // GetBrowserFlowCompleted returns the flag indicating whether user has completed the browser flow or not.
26- GetBrowserFlowCompleted () bool
27-
28- // SetBrowserFlowCompleted allows client to mark user has completed the browser flow.
29- SetBrowserFlowCompleted (flag bool )
30-
31- fosite.Session
32- }
33-
34- // DefaultDeviceFlowSession is a DeviceFlowSession implementation for the device flow.
35- type DefaultDeviceFlowSession struct {
36- ExpiresAt map [fosite.TokenType ]time.Time `json:"expires_at"`
37- Username string `json:"username"`
38- Subject string `json:"subject"`
39- Extra map [string ]interface {} `json:"extra"`
40- BrowserFlowCompleted bool `json:"browser_flow_completed"`
41- }
42-
43- func (s * DefaultDeviceFlowSession ) SetExpiresAt (key fosite.TokenType , exp time.Time ) {
44- if s .ExpiresAt == nil {
45- s .ExpiresAt = make (map [fosite.TokenType ]time.Time )
46- }
47- s .ExpiresAt [key ] = exp
48- }
49-
50- func (s * DefaultDeviceFlowSession ) GetExpiresAt (key fosite.TokenType ) time.Time {
51- if s .ExpiresAt == nil {
52- s .ExpiresAt = make (map [fosite.TokenType ]time.Time )
53- }
54-
55- if _ , ok := s .ExpiresAt [key ]; ! ok {
56- return time.Time {}
57- }
58- return s .ExpiresAt [key ]
59- }
60-
61- func (s * DefaultDeviceFlowSession ) GetUsername () string {
62- if s == nil {
63- return ""
64- }
65- return s .Username
66- }
67-
68- func (s * DefaultDeviceFlowSession ) SetSubject (subject string ) {
69- s .Subject = subject
70- }
71-
72- func (s * DefaultDeviceFlowSession ) GetSubject () string {
73- if s == nil {
74- return ""
75- }
76-
77- return s .Subject
78- }
79-
80- func (s * DefaultDeviceFlowSession ) Clone () fosite.Session {
81- if s == nil {
82- return nil
83- }
84-
85- return deepcopy .Copy (s ).(fosite.Session )
86- }
87-
88- func (s * DefaultDeviceFlowSession ) GetBrowserFlowCompleted () bool {
89- if s == nil {
90- return false
91- }
92-
93- return s .BrowserFlowCompleted
94- }
95-
96- func (s * DefaultDeviceFlowSession ) SetBrowserFlowCompleted (flag bool ) {
97- s .BrowserFlowCompleted = flag
98- }
99-
10021// DefaultDeviceStrategy implements the default device strategy
10122type DefaultDeviceStrategy struct {
10223 Enigma * enigma.HMACStrategy
@@ -129,7 +50,7 @@ func (h *DefaultDeviceStrategy) UserCodeSignature(ctx context.Context, token str
12950}
13051
13152// ValidateUserCode validates a user_code
132- func (h * DefaultDeviceStrategy ) ValidateUserCode (ctx context.Context , r fosite.Requester , code string ) error {
53+ func (h * DefaultDeviceStrategy ) ValidateUserCode (ctx context.Context , r fosite.DeviceRequester , code string ) error {
13354 exp := r .GetSession ().GetExpiresAt (fosite .UserCode )
13455 if exp .IsZero () && r .GetRequestedAt ().Add (h .Config .GetDeviceAndUserCodeLifespan (ctx )).Before (time .Now ().UTC ()) {
13556 return errorsx .WithStack (fosite .ErrDeviceExpiredToken .WithHintf ("User code expired at '%s'." , r .GetRequestedAt ().Add (h .Config .GetDeviceAndUserCodeLifespan (ctx ))))
@@ -156,7 +77,7 @@ func (h *DefaultDeviceStrategy) DeviceCodeSignature(ctx context.Context, token s
15677}
15778
15879// ValidateDeviceCode validates a device_code
159- func (h * DefaultDeviceStrategy ) ValidateDeviceCode (ctx context.Context , r fosite.Requester , code string ) error {
80+ func (h * DefaultDeviceStrategy ) ValidateDeviceCode (ctx context.Context , r fosite.DeviceRequester , code string ) error {
16081 exp := r .GetSession ().GetExpiresAt (fosite .DeviceCode )
16182 if exp .IsZero () && r .GetRequestedAt ().Add (h .Config .GetDeviceAndUserCodeLifespan (ctx )).Before (time .Now ().UTC ()) {
16283 return errorsx .WithStack (fosite .ErrDeviceExpiredToken .WithHintf ("Device code expired at '%s'." , r .GetRequestedAt ().Add (h .Config .GetDeviceAndUserCodeLifespan (ctx ))))
0 commit comments