-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoauth.go
52 lines (43 loc) · 1.07 KB
/
oauth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package hint
import (
"errors"
"time"
)
type OAuthParams struct {
GrantType string `json:"grant_type"`
Code string `json:"code"`
}
func (p *OAuthParams) Validate() error {
if p.Code == "" {
return errors.New("code required")
} else if p.GrantType == "" {
return errors.New("grant_type required")
}
return nil
}
type PracticeGrant struct {
ID string `json:"id"`
Status string `json:"status"`
TokenType string `json:"token_type"`
RefreshToken *string `json:"refresh_token"`
ExpiresIn *time.Time `json:"expires_in"`
Practice *Practice `json:"practice"`
AccessToken string `json:"access_token"`
}
type OAuthClient interface {
GrantAPIKey(code string) (*PracticeGrant, error)
}
type oauthClient struct {
B Backend
Key string
}
func (c oauthClient) GrantAPIKey(code string) (*PracticeGrant, error) {
var grant PracticeGrant
if _, err := c.B.Call("POST", "/oauth/tokens", Key, &OAuthParams{
GrantType: "authorization_code",
Code: code,
}, &grant); err != nil {
return nil, err
}
return &grant, nil
}