@@ -2,28 +2,54 @@ package mytokenlib
22
33import (
44 "github.com/oidc-mytoken/api/v0"
5- "github.com/oidc-mytoken/server/shared/httpClient"
65)
76
8- func (my * MytokenProvider ) GetAccessToken (mytoken , oidcIssuer string , scopes , audiences []string , comment string ) (string , error ) {
7+ // AccessTokenEndpoint is type representing a mytoken server's Access Token Endpoint and the actions that can be
8+ // performed there.
9+ type AccessTokenEndpoint struct {
10+ endpoint string
11+ }
12+
13+ func newAccessTokenEndpoint (endpoint string ) * AccessTokenEndpoint {
14+ return & AccessTokenEndpoint {
15+ endpoint : endpoint ,
16+ }
17+ }
18+
19+ // DoHTTPRequest performs an http request to the access token endpoint
20+ func (at AccessTokenEndpoint ) DoHTTPRequest (method string , req , resp interface {}) error {
21+ return doHTTPRequest (method , at .endpoint , req , resp )
22+ }
23+
24+ // APIGet uses the passed mytoken to return an access token with the specified attributes. If a non-empty string
25+ // is passed as the oidcIssuer it must match the oidc issuer of the mytoken. If scopes and audiences are passed the
26+ // access token is requested with these parameters, if omitted the default values for this mytoken / provider are used.
27+ // Multiple scopes are passed as a space separated string. The comment details how the access token is intended to be
28+ // used.
29+ // If the used mytoken changes (due to token rotation), the new mytoken is included in the api.AccessTokenResponse
30+ func (at AccessTokenEndpoint ) APIGet (
31+ mytoken string , oidcIssuer string , scopes , audiences []string , comment string ,
32+ ) (resp api.AccessTokenResponse , err error ) {
933 req := NewAccessTokenRequest (oidcIssuer , mytoken , scopes , audiences , comment )
10- resp , err := httpClient .Do ().R ().SetBody (req ).SetResult (& api.AccessTokenResponse {}).SetError (& api.Error {}).Post (my .AccessTokenEndpoint )
34+ err = at .DoHTTPRequest ("POST" , req , & resp )
35+ return
36+ }
37+
38+ // Get uses the passed mytoken to return an access token with the specified attributes. If a non-empty string
39+ // is passed as the oidcIssuer it must match the oidc issuer of the mytoken. If scopes and audiences are passed the
40+ // access token is requested with these parameters, if omitted the default values for this mytoken / provider are used.
41+ // Multiple scopes are passed as a space separated string. The comment details how the access token is intended to be
42+ // used.
43+ // If the used mytoken changes (due to token rotation), the passed variable is updated accordingly.
44+ func (at AccessTokenEndpoint ) Get (
45+ mytoken * string , oidcIssuer string , scopes , audiences []string , comment string ,
46+ ) (string , error ) {
47+ resp , err := at .APIGet (* mytoken , oidcIssuer , scopes , audiences , comment )
1148 if err != nil {
12- return "" , newMytokenErrorFromError ("error while sending http request" , err )
13- }
14- if e := resp .Error (); e != nil {
15- if errRes := e .(* api.Error ); errRes != nil && errRes .Error != "" {
16- return "" , & MytokenError {
17- err : errRes .Error ,
18- errorDetails : errRes .ErrorDescription ,
19- }
20- }
49+ return "" , err
2150 }
22- atRes , ok := resp .Result ().(* api.AccessTokenResponse )
23- if ! ok {
24- return "" , & MytokenError {
25- err : unexpectedResponse ,
26- }
51+ if resp .TokenUpdate != nil {
52+ * mytoken = resp .TokenUpdate .Mytoken
2753 }
28- return atRes .AccessToken , nil
54+ return resp .AccessToken , nil
2955}
0 commit comments