Skip to content

Commit a76cc65

Browse files
committed
don't use httpclient from server
1 parent 9dacea3 commit a76cc65

File tree

8 files changed

+143
-198
lines changed

8 files changed

+143
-198
lines changed

accesstoken.go

+5-20
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,13 @@ 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+
func (my *MytokenServer) GetAccessToken(mytoken, oidcIssuer string, scopes, audiences []string, comment string) (string, error) {
98
req := NewAccessTokenRequest(oidcIssuer, mytoken, scopes, audiences, comment)
10-
resp, err := httpClient.Do().R().SetBody(req).SetResult(&api.AccessTokenResponse{}).SetError(&api.Error{}).Post(my.AccessTokenEndpoint)
11-
if err != nil {
12-
return "", newMytokenErrorFromError("error while sending http request", err)
9+
var resp api.AccessTokenResponse
10+
if err := doHTTPRequest("POST", my.AccessTokenEndpoint, req, &resp); err != nil {
11+
return "", err
1312
}
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-
}
21-
}
22-
atRes, ok := resp.Result().(*api.AccessTokenResponse)
23-
if !ok {
24-
return "", &MytokenError{
25-
err: unexpectedResponse,
26-
}
27-
}
28-
return atRes.AccessToken, nil
13+
return resp.AccessToken, nil
2914
}

go.mod

+1-4
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.3.0

http.go

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package mytokenlib
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"net/http"
7+
8+
"github.com/oidc-mytoken/api/v0"
9+
)
10+
11+
const (
12+
errSendingHttpRequest = "error while sending http request"
13+
errDecodingHttpResponse = "could not decode response"
14+
errDecodingErrorResponse = "could not decode error response"
15+
errEncodingRequest = "could not encode request"
16+
)
17+
18+
const mimetypeJSON = "application/json"
19+
20+
func doHTTPRequest(method, url string, reqBody interface{}, responseData interface{}) *MytokenError {
21+
b := new(bytes.Buffer)
22+
if err := json.NewEncoder(b).Encode(reqBody); err != nil {
23+
return newMytokenErrorFromError(errEncodingRequest, err)
24+
}
25+
req, err := http.NewRequestWithContext(ctx, method, url, b)
26+
if err != nil {
27+
return newMytokenErrorFromError(errSendingHttpRequest, err)
28+
}
29+
if reqBody != nil {
30+
req.Header.Set("Content-Type", mimetypeJSON)
31+
}
32+
if responseData != nil {
33+
req.Header.Set("Accept", mimetypeJSON)
34+
}
35+
resp, err := httpClient.Do(req)
36+
if err != nil {
37+
return newMytokenErrorFromError(errSendingHttpRequest, err)
38+
}
39+
defer resp.Body.Close()
40+
if resp.StatusCode >= 400 {
41+
var apiError api.Error
42+
if err = json.NewDecoder(resp.Body).Decode(&apiError); err != nil {
43+
return newMytokenErrorFromError(errDecodingErrorResponse, err)
44+
}
45+
return &MytokenError{
46+
err: apiError.Error,
47+
errorDetails: apiError.ErrorDescription,
48+
}
49+
}
50+
if responseData != nil {
51+
if err = json.NewDecoder(resp.Body).Decode(responseData); err != nil {
52+
return newMytokenErrorFromError(errDecodingHttpResponse, err)
53+
}
54+
}
55+
return nil
56+
}

mytoken.go

+17-46
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,17 @@ import (
66
"time"
77

88
"github.com/oidc-mytoken/api/v0"
9-
"github.com/oidc-mytoken/server/shared/httpClient"
109
)
1110

12-
func (my *MytokenProvider) GetMytoken(req interface{}) (string, error) {
13-
resp, err := httpClient.Do().R().SetBody(req).SetResult(&api.MytokenResponse{}).SetError(&api.Error{}).Post(my.MytokenEndpoint)
14-
if err != nil {
15-
return "", newMytokenErrorFromError("error while sending http request", err)
16-
}
17-
if e := resp.Error(); e != nil {
18-
if errRes := e.(*api.Error); errRes != nil && errRes.Error != "" {
19-
return "", &MytokenError{
20-
err: errRes.Error,
21-
errorDetails: errRes.ErrorDescription,
22-
}
23-
}
24-
}
25-
stRes, ok := resp.Result().(*api.MytokenResponse)
26-
if !ok {
27-
return "", &MytokenError{
28-
err: "unexpected response from mytoken server",
29-
}
11+
func (my *MytokenServer) GetMytoken(req interface{}) (string, error) {
12+
var resp api.MytokenResponse
13+
if err := doHTTPRequest("POST", my.MytokenEndpoint, req, &resp); err != nil {
14+
return "", err
3015
}
31-
return stRes.Mytoken, nil
16+
return resp.Mytoken, nil
3217
}
3318

34-
func (my *MytokenProvider) GetMytokenByMytoken(mytoken, issuer string, restrictions api.Restrictions, capabilities, subtokenCapabilities api.Capabilities, responseType, name string) (string, error) {
19+
func (my *MytokenServer) GetMytokenByMytoken(mytoken, issuer string, restrictions api.Restrictions, capabilities, subtokenCapabilities api.Capabilities, responseType, name string) (string, error) {
3520
req := api.MytokenFromMytokenRequest{
3621
GeneralMytokenRequest: api.GeneralMytokenRequest{
3722
Issuer: issuer,
@@ -42,12 +27,12 @@ func (my *MytokenProvider) GetMytokenByMytoken(mytoken, issuer string, restricti
4227
Name: name,
4328
ResponseType: responseType,
4429
},
45-
Mytoken: mytoken,
30+
Mytoken: mytoken,
4631
}
4732
return my.GetMytoken(req)
4833
}
4934

50-
func (my *MytokenProvider) GetMytokenByTransferCode(transferCode string) (string, error) {
35+
func (my *MytokenServer) GetMytokenByTransferCode(transferCode string) (string, error) {
5136
req := api.ExchangeTransferCodeRequest{
5237
GrantType: api.GrantTypeTransferCode,
5338
TransferCode: transferCode,
@@ -61,7 +46,7 @@ type PollingCallbacks struct {
6146
End func()
6247
}
6348

64-
func (my *MytokenProvider) GetMytokenByAuthorizationFlow(issuer string, restrictions api.Restrictions, capabilities, subtokenCapabilities api.Capabilities, responseType, name string, callbacks PollingCallbacks) (string, error) {
49+
func (my *MytokenServer) GetMytokenByAuthorizationFlow(issuer string, restrictions api.Restrictions, capabilities, subtokenCapabilities api.Capabilities, responseType, name string, callbacks PollingCallbacks) (string, error) {
6550
authRes, err := my.InitAuthorizationFlow(issuer, restrictions, capabilities, subtokenCapabilities, responseType, name)
6651
if err != nil {
6752
return "", err
@@ -76,7 +61,7 @@ func (my *MytokenProvider) GetMytokenByAuthorizationFlow(issuer string, restrict
7661
return tok, err
7762
}
7863

79-
func (my *MytokenProvider) InitAuthorizationFlow(issuer string, restrictions api.Restrictions, capabilities, subtokenCapabilities api.Capabilities, responseType, name string) (*api.AuthCodeFlowResponse, error) {
64+
func (my *MytokenServer) InitAuthorizationFlow(issuer string, restrictions api.Restrictions, capabilities, subtokenCapabilities api.Capabilities, responseType, name string) (*api.AuthCodeFlowResponse, error) {
8065
req := api.AuthCodeFlowRequest{
8166
OIDCFlowRequest: api.OIDCFlowRequest{
8267
GeneralMytokenRequest: api.GeneralMytokenRequest{
@@ -88,32 +73,18 @@ func (my *MytokenProvider) InitAuthorizationFlow(issuer string, restrictions api
8873
Name: name,
8974
ResponseType: responseType,
9075
},
91-
OIDCFlow: api.OIDCFlowAuthorizationCode,
76+
OIDCFlow: api.OIDCFlowAuthorizationCode,
9277
},
9378
RedirectType: "native",
9479
}
95-
resp, err := httpClient.Do().R().SetBody(req).SetResult(&api.AuthCodeFlowResponse{}).SetError(&api.Error{}).Post(my.MytokenEndpoint)
96-
if err != nil {
97-
return nil, newMytokenErrorFromError("error while sending http request", err)
98-
}
99-
if e := resp.Error(); e != nil {
100-
if errRes := e.(*api.Error); errRes != nil && errRes.Error != "" {
101-
return nil, &MytokenError{
102-
err: errRes.Error,
103-
errorDetails: errRes.ErrorDescription,
104-
}
105-
}
106-
}
107-
authRes, ok := resp.Result().(*api.AuthCodeFlowResponse)
108-
if !ok {
109-
return nil, &MytokenError{
110-
err: unexpectedResponse,
111-
}
80+
var resp api.AuthCodeFlowResponse
81+
if err := doHTTPRequest("POST", my.MytokenEndpoint, req, &resp); err != nil {
82+
return nil, err
11283
}
113-
return authRes, nil
84+
return &resp, nil
11485
}
11586

116-
func (my *MytokenProvider) Poll(res api.PollingInfo, callback func(int64, int)) (string, error) {
87+
func (my *MytokenServer) Poll(res api.PollingInfo, callback func(int64, int)) (string, error) {
11788
expires := time.Now().Add(time.Duration(res.PollingCodeExpiresIn) * time.Second)
11889
interval := res.PollingInterval
11990
if interval == 0 {
@@ -139,7 +110,7 @@ func (my *MytokenProvider) Poll(res api.PollingInfo, callback func(int64, int))
139110
return "", fmt.Errorf("polling code expired")
140111
}
141112

142-
func (my *MytokenProvider) PollOnce(pollingCode string) (string, bool, error) {
113+
func (my *MytokenServer) PollOnce(pollingCode string) (string, bool, error) {
143114
req := api.PollingCodeRequest{
144115
GrantType: api.GrantTypePollingCode,
145116
PollingCode: pollingCode,

provider.go

-36
This file was deleted.

revoke.go

+2-15
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,13 @@ 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) Revoke(mytoken, oidcIssuer string, recursive bool) error {
7+
func (my *MytokenServer) Revoke(mytoken, oidcIssuer string, recursive bool) error {
98
req := api.RevocationRequest{
109
Token: mytoken,
1110
Recursive: recursive,
1211
OIDCIssuer: oidcIssuer,
1312
}
14-
resp, err := httpClient.Do().R().SetBody(req).SetError(&api.Error{}).Post(my.RevocationEndpoint)
15-
if err != nil {
16-
return newMytokenErrorFromError("error while sending http request", err)
17-
}
18-
if e := resp.Error(); e != nil {
19-
if errRes := e.(*api.Error); errRes != nil && errRes.Error != "" {
20-
return &MytokenError{
21-
err: errRes.Error,
22-
errorDetails: errRes.ErrorDescription,
23-
}
24-
}
25-
}
26-
return nil
13+
return doHTTPRequest("POST", my.RevocationEndpoint, req, nil)
2714
}

server.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package mytokenlib
2+
3+
import (
4+
"context"
5+
"net/http"
6+
7+
"github.com/oidc-mytoken/api/v0"
8+
)
9+
10+
// MytokenServer is a type describing a mytoken server instance
11+
type MytokenServer struct {
12+
api.MytokenConfiguration
13+
}
14+
15+
var httpClient = &http.Client{}
16+
var ctx = context.Background()
17+
18+
// NewMytokenServer creates a new MytokenServer
19+
func NewMytokenServer(url string) (*MytokenServer, error) {
20+
configEndpoint := url
21+
if url[len(url)-1] != '/' {
22+
configEndpoint += "/"
23+
}
24+
configEndpoint += ".well-known/mytoken-configuration"
25+
var respData api.MytokenConfiguration
26+
if err := doHTTPRequest("GET", configEndpoint, nil, &respData); err != nil {
27+
return nil, err
28+
}
29+
return &MytokenServer{
30+
MytokenConfiguration: respData,
31+
}, nil
32+
}
33+
34+
// SetClient sets the http.Client used to make API requests
35+
func SetClient(client *http.Client) {
36+
httpClient = client
37+
}
38+
39+
// SetContext sets a context.Context used for all API requests
40+
func SetContext(contxt context.Context) {
41+
ctx = contxt
42+
}

0 commit comments

Comments
 (0)