Skip to content

Commit fb2b8ff

Browse files
committed
add comments; cleanup formatting
1 parent b7cbd16 commit fb2b8ff

File tree

7 files changed

+73
-15
lines changed

7 files changed

+73
-15
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
`mytokenlib` is a go library for communicating with amytoken server.
1313
`mytoken` is a central web service with the goal to easily obtain OpenID Connect access tokens across devices.
1414

15-
A mytoken command line client can be found at [https://github.com/oidc-mytoken/client](https://github.com/oidc-mytoken/client).
15+
A mytoken command line client can be found
16+
at [https://github.com/oidc-mytoken/client](https://github.com/oidc-mytoken/client).
1617

1718
The mytoken server can be found at [https://github.com/oidc-mytoken/server](https://github.com/oidc-mytoken/server).
1819

accesstoken.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@ import (
44
"github.com/oidc-mytoken/api/v0"
55
)
66

7-
func (my *MytokenServer) GetAccessToken(mytoken *string, oidcIssuer string, scopes, audiences []string, comment string) (string, error) {
7+
// GetAccessToken uses the passed mytoken to return an access token with the specified attributes. If a non-empty string
8+
// is passed as the oidcIssuer it must match the oidc issuer of the mytoken. If scopes and audiences are passed the
9+
// access token is requested with these parameters, if omitted the default values for this mytoken / provider are used.
10+
// Multiple scopes are passed as a space separated string. The comment details how the access token is intended to be
11+
// used.
12+
// If the used mytoken changes (due to token rotation), the passed variable is updated accordingly.
13+
func (my *MytokenServer) GetAccessToken(
14+
mytoken *string, oidcIssuer string, scopes, audiences []string, comment string,
15+
) (string, error) {
816
req := NewAccessTokenRequest(oidcIssuer, *mytoken, scopes, audiences, comment)
917
var resp api.AccessTokenResponse
1018
if err := doHTTPRequest("POST", my.AccessTokenEndpoint, req, &resp); err != nil {

error.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ type MytokenError struct {
66
errorDetails string
77
}
88

9+
// Error implements the error interface and returns a string representation of this MytokenError
910
func (err *MytokenError) Error() string {
1011
e := err.err
1112
if err.errorDetails != "" {
@@ -20,6 +21,3 @@ func newMytokenErrorFromError(e string, err error) *MytokenError {
2021
errorDetails: err.Error(),
2122
}
2223
}
23-
24-
const unexpectedResponse = "unexpected response from mytoken server"
25-
const errorWhileHttp = "error while sending http request"

mytoken.go

+46-9
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,26 @@ import (
88
"github.com/oidc-mytoken/api/v0"
99
)
1010

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) {
1214
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 {
1416
return "", nil, err
1517
}
16-
var mtUpdate *string
18+
var updatedMT *string
1719
if resp.TokenUpdate != nil {
18-
mtUpdate = &resp.TokenUpdate.Mytoken
20+
updatedMT = &resp.TokenUpdate.Mytoken
1921
}
20-
return resp.Mytoken, mtUpdate, nil
22+
return resp.Mytoken, updatedMT, nil
2123
}
2224

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) {
2431
req := api.MytokenFromMytokenRequest{
2532
GeneralMytokenRequest: api.GeneralMytokenRequest{
2633
Issuer: issuer,
@@ -40,6 +47,7 @@ func (my *MytokenServer) GetMytokenByMytoken(mytoken *string, issuer string, res
4047
return mt, err
4148
}
4249

50+
// GetMytokenByTransferCode exchanges the transferCode into the linked mytoken
4351
func (my *MytokenServer) GetMytokenByTransferCode(transferCode string) (string, error) {
4452
req := api.ExchangeTransferCodeRequest{
4553
GrantType: api.GrantTypeTransferCode,
@@ -49,14 +57,30 @@ func (my *MytokenServer) GetMytokenByTransferCode(transferCode string) (string,
4957
return mt, err
5058
}
5159

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.
5268
type PollingCallbacks struct {
5369
Init func(string) error
5470
Callback func(int64, int)
5571
End func()
5672
}
5773

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+
)
6084
if err != nil {
6185
return "", err
6286
}
@@ -70,7 +94,12 @@ func (my *MytokenServer) GetMytokenByAuthorizationFlow(issuer string, restrictio
7094
return tok, err
7195
}
7296

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) {
74103
req := api.AuthCodeFlowRequest{
75104
OIDCFlowRequest: api.OIDCFlowRequest{
76105
GeneralMytokenRequest: api.GeneralMytokenRequest{
@@ -93,6 +122,12 @@ func (my *MytokenServer) InitAuthorizationFlow(issuer string, restrictions api.R
93122
return &resp, nil
94123
}
95124

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.
96131
func (my *MytokenServer) Poll(res api.PollingInfo, callback func(int64, int)) (string, error) {
97132
expires := time.Now().Add(time.Duration(res.PollingCodeExpiresIn) * time.Second)
98133
interval := res.PollingInterval
@@ -119,6 +154,8 @@ func (my *MytokenServer) Poll(res api.PollingInfo, callback func(int64, int)) (s
119154
return "", fmt.Errorf("polling code expired")
120155
}
121156

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.
122159
func (my *MytokenServer) PollOnce(pollingCode string) (string, bool, error) {
123160
req := api.PollingCodeRequest{
124161
GrantType: api.GrantTypePollingCode,

requests.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package mytokenlib
33
import (
44
"strings"
55

6-
api "github.com/oidc-mytoken/api/v0"
6+
"github.com/oidc-mytoken/api/v0"
77
)
88

9+
// NewAccessTokenRequest creates a new api.AccessTokenRequest with the passed arguments
910
func NewAccessTokenRequest(issuer, mytoken string, scopes, audiences []string, comment string) api.AccessTokenRequest {
1011
return api.AccessTokenRequest{
1112
Issuer: issuer,

revoke.go

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/oidc-mytoken/api/v0"
55
)
66

7+
// Revoke revokes the passed mytoken; if recursive is true also all subtokens (and their subtokens...) are revoked.
78
func (my *MytokenServer) Revoke(mytoken string, oidcIssuer string, recursive bool) error {
89
req := api.RevocationRequest{
910
Token: mytoken,

tokeninfo.go

+12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/oidc-mytoken/api/v0"
55
)
66

7+
// TokeninfoIntrospect introspects the passed mytoken
78
func (my *MytokenServer) TokeninfoIntrospect(mytoken string) (*api.TokeninfoIntrospectResponse, error) {
89
req := api.TokenInfoRequest{
910
Action: api.TokeninfoActionIntrospect,
@@ -15,6 +16,9 @@ func (my *MytokenServer) TokeninfoIntrospect(mytoken string) (*api.TokeninfoIntr
1516
}
1617
return &resp, nil
1718
}
19+
20+
// TokeninfoHistory obtains the event history for the passed mytoken.
21+
// If the used mytoken changes (due to token rotation), the passed variable is updated accordingly.
1822
func (my *MytokenServer) TokeninfoHistory(mytoken *string) (api.EventHistory, error) {
1923
req := api.TokenInfoRequest{
2024
Action: api.TokeninfoActionEventHistory,
@@ -29,6 +33,10 @@ func (my *MytokenServer) TokeninfoHistory(mytoken *string) (api.EventHistory, er
2933
}
3034
return resp.EventHistory, nil
3135
}
36+
37+
// TokeninfoSubtokens returns a api.MytokenEntryTree listing metadata about the passed mytoken and its children (
38+
// recursively)
39+
// If the used mytoken changes (due to token rotation), the passed variable is updated accordingly.
3240
func (my *MytokenServer) TokeninfoSubtokens(mytoken *string) (*api.MytokenEntryTree, error) {
3341
req := api.TokenInfoRequest{
3442
Action: api.TokeninfoActionSubtokenTree,
@@ -43,6 +51,10 @@ func (my *MytokenServer) TokeninfoSubtokens(mytoken *string) (*api.MytokenEntryT
4351
}
4452
return &resp.Tokens, nil
4553
}
54+
55+
// TokeninfoListMytokens returns a slice of api.MytokenEntryTree listing metadata about all the user's mytoken and their
56+
// children (recursively)
57+
// If the used mytoken changes (due to token rotation), the passed variable is updated accordingly.
4658
func (my *MytokenServer) TokeninfoListMytokens(mytoken *string) ([]api.MytokenEntryTree, error) {
4759
req := api.TokenInfoRequest{
4860
Action: api.TokeninfoActionListMytokens,

0 commit comments

Comments
 (0)