Skip to content

Commit 35c89f9

Browse files
authored
Merge pull request #2 from oidc-mytoken/dev
Dev
2 parents 9dacea3 + cc70eba commit 35c89f9

18 files changed

+882
-1002
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020-2021 Gabriel Zachmann
3+
Copyright (c) 2020-2022 Gabriel Zachmann
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 2 additions & 1 deletion
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

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,54 @@ package mytokenlib
22

33
import (
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
}

error.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,18 @@ type MytokenError struct {
66
errorDetails string
77
}
88

9-
func (err *MytokenError) Error() string {
9+
// Error implements the error interface and returns a string representation of this MytokenError
10+
func (err MytokenError) Error() string {
1011
e := err.err
1112
if err.errorDetails != "" {
1213
e += ": " + err.errorDetails
1314
}
1415
return e
1516
}
1617

17-
func newMytokenErrorFromError(e string, err error) *MytokenError {
18-
return &MytokenError{
18+
func newMytokenErrorFromError(e string, err error) MytokenError {
19+
return MytokenError{
1920
err: e,
2021
errorDetails: err.Error(),
2122
}
2223
}
23-
24-
const unexpectedResponse = "unexpected response from mytoken server"
25-
const errorWhileHttp = "error while sending http request"

go.mod

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,4 @@ module github.com/oidc-mytoken/lib
22

33
go 1.13
44

5-
require (
6-
github.com/oidc-mytoken/api v0.3.0
7-
github.com/oidc-mytoken/server v0.2.0
8-
)
5+
require github.com/oidc-mytoken/api v0.4.0

0 commit comments

Comments
 (0)