This repository has been archived by the owner on Feb 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.go
90 lines (69 loc) · 2.36 KB
/
app.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package tent
import (
"crypto/sha256"
"encoding/json"
"net/http"
"github.com/tent/hawk-go"
)
const (
PostTypeApp = "https://tent.io/types/app/v0#"
PostTypeAppAuth = "https://tent.io/types/app-auth/v0#"
PostTypeCredentials = "https://tent.io/types/credentials/v0#"
)
type Credentials struct {
HawkKey string `json:"hawk_key"`
HawkAlgorithm string `json:"hawk_algorithm"`
Post *Post
}
func NewAppPost(app *App) *Post {
data, _ := json.Marshal(app)
return &Post{Type: PostTypeApp, Content: data, Permissions: &PostPermissions{PublicFlag: new(bool)}}
}
type AppTypes struct {
Read []string `json:"read,omitempty"`
Write []string `json:"write,omitempty"`
}
type App struct {
Name string `json:"name"`
URL string `json:"url"`
Description string `json:"description,omitempty"`
Scopes []string `json:"scopes,omitempty"`
Types AppTypes `json:"types,omitempty"`
RedirectURI string `json:"redirect_uri"`
NotificationURL string `json:"notification_url,omitempty"`
NotificationTypes []string `json:"notification_types,omitempty"`
Post *Post `json:"-"`
}
type AppAuth struct {
Active bool `json:"active"`
Scopes []string `json:"scopes,omitempty"`
Types AppTypes `json:"types,omitempty"`
Post *Post `json:"-"`
}
const TokenTypeHawk = "https://tent.io/oauth/hawk-token"
func oauthTokenURL(server *MetaPostServer) string { return server.URLs.OAuthToken }
func (client *Client) RequestAccessToken(code string) (*hawk.Credentials, error) {
data, _ := json.Marshal(&AccessTokenRequest{TokenType: TokenTypeHawk, Code: code})
tokenRes := &AccessTokenResponse{}
header := make(http.Header)
header.Set("Accept", "application/json")
header.Set("Content-Type", "application/json")
_, err := client.requestJSON("POST", oauthTokenURL, header, data, tokenRes)
if err != nil {
return nil, err
}
return tokenRes.HawkCredentials(client.Credentials.App), err
}
type AccessTokenRequest struct {
Code string `json:"code"`
TokenType string `json:"token_type"`
}
type AccessTokenResponse struct {
HawkID string `json:"access_token"`
HawkKey string `json:"hawk_key"`
HawkAlgorithm string `json:"hawk_algorithm"`
TokenType string `json:"token_type"`
}
func (res *AccessTokenResponse) HawkCredentials(app string) *hawk.Credentials {
return &hawk.Credentials{Key: res.HawkKey, ID: res.HawkID, App: app, Hash: sha256.New}
}