Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 75ca5cd

Browse files
echoesactiiipascoual
authored andcommitted
Add generic oauth module
Signed-off-by: prichier <[email protected]>
1 parent ee1ca48 commit 75ca5cd

1 file changed

Lines changed: 114 additions & 0 deletions

File tree

oauth_generic.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package writefreely
2+
3+
import (
4+
"context"
5+
"errors"
6+
"net/http"
7+
"net/url"
8+
"strings"
9+
)
10+
11+
type genericOauthClient struct {
12+
ClientID string
13+
ClientSecret string
14+
AuthLocation string
15+
ExchangeLocation string
16+
InspectLocation string
17+
CallbackLocation string
18+
HttpClient HttpClient
19+
}
20+
21+
var _ oauthClient = genericOauthClient{}
22+
23+
const (
24+
genericOauthDisplayName = "oAuth"
25+
)
26+
27+
func (c genericOauthClient) GetProvider() string {
28+
return "generic"
29+
}
30+
31+
func (c genericOauthClient) GetClientID() string {
32+
return c.ClientID
33+
}
34+
35+
func (c genericOauthClient) GetCallbackLocation() string {
36+
return c.CallbackLocation
37+
}
38+
39+
func (c genericOauthClient) buildLoginURL(state string) (string, error) {
40+
u, err := url.Parse(c.AuthLocation)
41+
if err != nil {
42+
return "", err
43+
}
44+
q := u.Query()
45+
q.Set("client_id", c.ClientID)
46+
q.Set("redirect_uri", c.CallbackLocation)
47+
q.Set("response_type", "code")
48+
q.Set("state", state)
49+
q.Set("scope", "read_user")
50+
u.RawQuery = q.Encode()
51+
return u.String(), nil
52+
}
53+
54+
func (c genericOauthClient) exchangeOauthCode(ctx context.Context, code string) (*TokenResponse, error) {
55+
form := url.Values{}
56+
form.Add("grant_type", "authorization_code")
57+
form.Add("redirect_uri", c.CallbackLocation)
58+
form.Add("scope", "read_user")
59+
form.Add("code", code)
60+
req, err := http.NewRequest("POST", c.ExchangeLocation, strings.NewReader(form.Encode()))
61+
if err != nil {
62+
return nil, err
63+
}
64+
req.WithContext(ctx)
65+
req.Header.Set("User-Agent", "writefreely")
66+
req.Header.Set("Accept", "application/json")
67+
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
68+
req.SetBasicAuth(c.ClientID, c.ClientSecret)
69+
70+
resp, err := c.HttpClient.Do(req)
71+
if err != nil {
72+
return nil, err
73+
}
74+
if resp.StatusCode != http.StatusOK {
75+
return nil, errors.New("unable to exchange code for access token")
76+
}
77+
78+
var tokenResponse TokenResponse
79+
if err := limitedJsonUnmarshal(resp.Body, tokenRequestMaxLen, &tokenResponse); err != nil {
80+
return nil, err
81+
}
82+
if tokenResponse.Error != "" {
83+
return nil, errors.New(tokenResponse.Error)
84+
}
85+
return &tokenResponse, nil
86+
}
87+
88+
func (c genericOauthClient) inspectOauthAccessToken(ctx context.Context, accessToken string) (*InspectResponse, error) {
89+
req, err := http.NewRequest("GET", c.InspectLocation, nil)
90+
if err != nil {
91+
return nil, err
92+
}
93+
req.WithContext(ctx)
94+
req.Header.Set("User-Agent", "writefreely")
95+
req.Header.Set("Accept", "application/json")
96+
req.Header.Set("Authorization", "Bearer "+accessToken)
97+
98+
resp, err := c.HttpClient.Do(req)
99+
if err != nil {
100+
return nil, err
101+
}
102+
if resp.StatusCode != http.StatusOK {
103+
return nil, errors.New("unable to inspect access token")
104+
}
105+
106+
var inspectResponse InspectResponse
107+
if err := limitedJsonUnmarshal(resp.Body, infoRequestMaxLen, &inspectResponse); err != nil {
108+
return nil, err
109+
}
110+
if inspectResponse.Error != "" {
111+
return nil, errors.New(inspectResponse.Error)
112+
}
113+
return &inspectResponse, nil
114+
}

0 commit comments

Comments
 (0)