@@ -8,19 +8,26 @@ import (
8
8
"github.com/oidc-mytoken/api/v0"
9
9
)
10
10
11
- func (my * MytokenServer ) GetMytoken (req interface {}) (string , * string , error ) {
11
+ // GetMytoken sends the passed request marshalled as json to the servers mytoken endpoint to obtain a mytoken and
12
+ // returns the obtained mytoken and if a mytoken was used for authorization and it was rotated the updated mytoken.
13
+ func (my * MytokenServer ) GetMytoken (request interface {}) (string , * string , error ) {
12
14
var resp api.MytokenResponse
13
- if err := doHTTPRequest ("POST" , my .MytokenEndpoint , req , & resp ); err != nil {
15
+ if err := doHTTPRequest ("POST" , my .MytokenEndpoint , request , & resp ); err != nil {
14
16
return "" , nil , err
15
17
}
16
- var mtUpdate * string
18
+ var updatedMT * string
17
19
if resp .TokenUpdate != nil {
18
- mtUpdate = & resp .TokenUpdate .Mytoken
20
+ updatedMT = & resp .TokenUpdate .Mytoken
19
21
}
20
- return resp .Mytoken , mtUpdate , nil
22
+ return resp .Mytoken , updatedMT , nil
21
23
}
22
24
23
- func (my * MytokenServer ) GetMytokenByMytoken (mytoken * string , issuer string , restrictions api.Restrictions , capabilities , subtokenCapabilities api.Capabilities , responseType , name string ) (string , error ) {
25
+ // GetMytokenByMytoken obtains a sub-mytoken by using an existing mytoken according to the passed parameters.
26
+ // If the used mytoken changes (due to token rotation), the passed variable is updated accordingly.
27
+ func (my * MytokenServer ) GetMytokenByMytoken (
28
+ mytoken * string , issuer string , restrictions api.Restrictions , capabilities , subtokenCapabilities api.Capabilities ,
29
+ responseType , name string ,
30
+ ) (string , error ) {
24
31
req := api.MytokenFromMytokenRequest {
25
32
GeneralMytokenRequest : api.GeneralMytokenRequest {
26
33
Issuer : issuer ,
@@ -40,6 +47,7 @@ func (my *MytokenServer) GetMytokenByMytoken(mytoken *string, issuer string, res
40
47
return mt , err
41
48
}
42
49
50
+ // GetMytokenByTransferCode exchanges the transferCode into the linked mytoken
43
51
func (my * MytokenServer ) GetMytokenByTransferCode (transferCode string ) (string , error ) {
44
52
req := api.ExchangeTransferCodeRequest {
45
53
GrantType : api .GrantTypeTransferCode ,
@@ -49,14 +57,30 @@ func (my *MytokenServer) GetMytokenByTransferCode(transferCode string) (string,
49
57
return mt , err
50
58
}
51
59
60
+ // PollingCallbacks is a struct holding callback related to the polling in the authorization code flow.
61
+ // The Init function takes the authorization url and is called before the starting polling the server; this callback
62
+ // usually displays information to the user how to proceed, including the passed authorization url
63
+ // The Callback function takes the polling interval and the number of iteration as parameters; it is called for each
64
+ // polling attempt where the final mytoken could not yet be obtained (but no error occurred); it is usually used to
65
+ // print progress output.
66
+ // The End function is called after the mytoken was successfully obtained and might be used to finish output printed
67
+ // to the user.
52
68
type PollingCallbacks struct {
53
69
Init func (string ) error
54
70
Callback func (int64 , int )
55
71
End func ()
56
72
}
57
73
58
- func (my * MytokenServer ) GetMytokenByAuthorizationFlow (issuer string , restrictions api.Restrictions , capabilities , subtokenCapabilities api.Capabilities , responseType , name string , callbacks PollingCallbacks ) (string , error ) {
59
- authRes , err := my .InitAuthorizationFlow (issuer , restrictions , capabilities , subtokenCapabilities , responseType , name )
74
+ // GetMytokenByAuthorizationFlow is a rather high level function that obtains a new mytoken using the authorization
75
+ // code flow. This function starts the flow with the passed parameters and performs the polling for the mytoken.
76
+ // The passed PollingCallbacks are called throughout the flow.
77
+ func (my * MytokenServer ) GetMytokenByAuthorizationFlow (
78
+ issuer string , restrictions api.Restrictions , capabilities , subtokenCapabilities api.Capabilities ,
79
+ responseType , name string , callbacks PollingCallbacks ,
80
+ ) (string , error ) {
81
+ authRes , err := my .InitAuthorizationFlow (
82
+ issuer , restrictions , capabilities , subtokenCapabilities , responseType , name ,
83
+ )
60
84
if err != nil {
61
85
return "" , err
62
86
}
@@ -70,7 +94,12 @@ func (my *MytokenServer) GetMytokenByAuthorizationFlow(issuer string, restrictio
70
94
return tok , err
71
95
}
72
96
73
- func (my * MytokenServer ) InitAuthorizationFlow (issuer string , restrictions api.Restrictions , capabilities , subtokenCapabilities api.Capabilities , responseType , name string ) (* api.AuthCodeFlowResponse , error ) {
97
+ // InitAuthorizationFlow starts the authorization code flow to obtain a mytoken with the passed parameters; it
98
+ // returns the api.AuthCodeFlowResponse
99
+ func (my * MytokenServer ) InitAuthorizationFlow (
100
+ issuer string , restrictions api.Restrictions , capabilities , subtokenCapabilities api.Capabilities ,
101
+ responseType , name string ,
102
+ ) (* api.AuthCodeFlowResponse , error ) {
74
103
req := api.AuthCodeFlowRequest {
75
104
OIDCFlowRequest : api.OIDCFlowRequest {
76
105
GeneralMytokenRequest : api.GeneralMytokenRequest {
@@ -93,6 +122,12 @@ func (my *MytokenServer) InitAuthorizationFlow(issuer string, restrictions api.R
93
122
return & resp , nil
94
123
}
95
124
125
+ // Poll performs the polling for the final mytoken in the authorization code flow using the passed api.
126
+ // PollingInfo.
127
+ // The callback function takes the polling interval and the number of iteration as parameters; it is called for each
128
+ // polling attempt where the final mytoken could not yet be obtained (but no error occurred); it is usually used to
129
+ // print progress output.
130
+ // At the end the mytoken is returned.
96
131
func (my * MytokenServer ) Poll (res api.PollingInfo , callback func (int64 , int )) (string , error ) {
97
132
expires := time .Now ().Add (time .Duration (res .PollingCodeExpiresIn ) * time .Second )
98
133
interval := res .PollingInterval
@@ -119,6 +154,8 @@ func (my *MytokenServer) Poll(res api.PollingInfo, callback func(int64, int)) (s
119
154
return "" , fmt .Errorf ("polling code expired" )
120
155
}
121
156
157
+ // PollOnce sends a single polling request with the passed pollingCode; it returns the mytoken if obtained,
158
+ // a bool indicating if the mytoken was obtained, or an error if an error occurred.
122
159
func (my * MytokenServer ) PollOnce (pollingCode string ) (string , bool , error ) {
123
160
req := api.PollingCodeRequest {
124
161
GrantType : api .GrantTypePollingCode ,
0 commit comments