This repository was archived by the owner on Nov 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadn.go
More file actions
143 lines (130 loc) · 3.49 KB
/
adn.go
File metadata and controls
143 lines (130 loc) · 3.49 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package adn
import (
"errors"
"html/template"
"io"
"net/http"
"net/url"
"strings"
)
const (
SCOPE_BASIC = "basic"
SCOPE_STREAM = "stream"
SCOPE_WRITE_POST = "write_post"
SCOPE_FOLLOW = "follow"
SCOPE_PUBLIC_MESSAGES = "public_messages"
SCOPE_MESSAGES = "messages"
SCOPE_UPDATE_PROFILE = "update_profile"
SCOPE_FILES = "files"
SCOPE_EXPORT = "export"
)
type ADN struct {
Token string
Scopes []string
ClientID string
ClientSecret string
RedirectURI string
}
var TokenNotSet = errors.New("Access token not set.")
var NoScopesSet = errors.New("Scopes not set.")
var NoClientIDSet = errors.New("Client ID not set.")
var NoRedirectURISet = errors.New("Redirect URI not set.")
func NewClient(client_id, client_secret, redirect_uri string, scopes []string) *ADN {
return &ADN{
Scopes: scopes,
ClientID: client_id,
ClientSecret: client_secret,
RedirectURI: redirect_uri,
}
}
func (a *ADN) makeURL(endpoint string) string {
endpoint = strings.TrimLeft(endpoint, "/")
return "https://alpha-api.app.net/" + endpoint
}
func (a *ADN) makeRequest(method, urlStr string, body io.Reader) (*http.Request, error) {
if a.Token == "" {
return nil, TokenNotSet
}
req, err := http.NewRequest(method, urlStr, body)
if err != nil {
return req, err
}
req.Header.Add("Authorization", "Bearer "+a.Token)
req.Header.Add("Content-Type", "application/json")
return req, nil
}
func (a *ADN) GetClientSideAuthURL() (string, error) {
if len(a.Scopes) == 0 {
return "", NoScopesSet
}
if a.ClientID == "" {
return "", NoClientIDSet
}
if a.RedirectURI == "" {
return "", NoRedirectURISet
}
params := make(url.Values)
params.Add("client_id", a.ClientID)
params.Add("response_type", "token")
params.Add("redirect_uri", a.RedirectURI)
params.Add("scope", strings.Join(a.Scopes, " "))
return "https://account.app.net/oauth/authenticate?" + params.Encode(), nil
}
type clientSideAuthListener struct {
listener chan string
}
func (c *clientSideAuthListener) ServeHTTP(w http.ResponseWriter, req *http.Request) {
token := req.FormValue("access_token")
if token == "" {
io.WriteString(w, "Access token not found in the request fragment.")
return
}
c.listener <- token
io.WriteString(w, "Successfully obtained access token. You can close this window.")
}
var redirectPage = `<html>
<head>
<script>
window.location = "/auth/token?" + window.location.hash.substr(1, window.location.hash.length - 1);
</script>
</head>
<body>
You should be redirected momentarily.
</body>
</html>`
func ServeRedirect(w http.ResponseWriter, req *http.Request) {
tmp, err := template.New("redirect").Parse(redirectPage)
if err != nil {
io.WriteString(w, "Error parsing redirect template.\n"+err.Error()+"\n")
}
err = tmp.Execute(w, nil)
if err != nil {
io.WriteString(w, "Error executing template.\n"+err.Error()+"\n")
}
}
func (a *ADN) ListenForClientSideAuth() (string, error) {
if a.RedirectURI == "" {
return "", NoRedirectURISet
}
red_url, err := url.Parse(a.RedirectURI)
if err != nil {
return "", err
}
port := ":80"
colIndex := strings.Index(red_url.Host, ":")
if colIndex > -1 {
port = red_url.Host[colIndex:]
}
authListener := new(clientSideAuthListener)
authListener.listener = make(chan string)
go func() {
http.Handle("/auth/token", authListener)
http.HandleFunc("/", ServeRedirect)
err := http.ListenAndServe(port, nil)
if err != nil {
panic(err)
}
}()
token := <-authListener.listener
return token, nil
}