-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmain.go
More file actions
171 lines (158 loc) · 4.7 KB
/
main.go
File metadata and controls
171 lines (158 loc) · 4.7 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"crypto/rand"
"encoding/base64"
"flag"
"fmt"
"github.com/kurrik/oauth1a"
"io"
"log"
"net/http"
"os"
)
var (
settings *Settings
service *oauth1a.Service
sessions map[string]*oauth1a.UserConfig
)
func NewSessionID() string {
c := 128
b := make([]byte, c)
n, err := io.ReadFull(rand.Reader, b)
if n != len(b) || err != nil {
panic("Could not generate random number")
}
return base64.URLEncoding.EncodeToString(b)
}
func GetSessionID(req *http.Request) (id string, err error) {
var c *http.Cookie
if c, err = req.Cookie("session_id"); err != nil {
return
}
id = c.Value
return
}
func SessionStartCookie(id string) *http.Cookie {
return &http.Cookie{
Name: "session_id",
Value: id,
MaxAge: 60,
Secure: false,
Path: "/",
}
}
func SessionEndCookie() *http.Cookie {
return &http.Cookie{
Name: "session_id",
Value: "",
MaxAge: 0,
Secure: false,
Path: "/",
}
}
func BaseHandler(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set("Content-Type", "text/html;charset=utf-8")
fmt.Fprintf(rw, "<a href=\"/signin\">Sign in</a>")
}
func SignInHandler(rw http.ResponseWriter, req *http.Request) {
var (
url string
err error
sessionID string
)
httpClient := new(http.Client)
userConfig := &oauth1a.UserConfig{}
if err = userConfig.GetRequestToken(service, httpClient); err != nil {
log.Printf("Could not get request token: %v", err)
http.Error(rw, "Problem getting the request token", 500)
return
}
if url, err = userConfig.GetAuthorizeURL(service); err != nil {
log.Printf("Could not get authorization URL: %v", err)
http.Error(rw, "Problem getting the authorization URL", 500)
return
}
log.Printf("Redirecting user to %v\n", url)
sessionID = NewSessionID()
log.Printf("Starting session %v\n", sessionID)
sessions[sessionID] = userConfig
http.SetCookie(rw, SessionStartCookie(sessionID))
http.Redirect(rw, req, url, 302)
}
func CallbackHandler(rw http.ResponseWriter, req *http.Request) {
var (
err error
token string
verifier string
sessionID string
userConfig *oauth1a.UserConfig
ok bool
)
log.Printf("Callback hit. %v current sessions.\n", len(sessions))
if sessionID, err = GetSessionID(req); err != nil {
log.Printf("Got a callback with no session id: %v\n", err)
http.Error(rw, "No session found", 400)
return
}
if userConfig, ok = sessions[sessionID]; !ok {
log.Printf("Could not find user config in sesions storage.")
http.Error(rw, "Invalid session", 400)
return
}
if token, verifier, err = userConfig.ParseAuthorize(req, service); err != nil {
log.Printf("Could not parse authorization: %v", err)
http.Error(rw, "Problem parsing authorization", 500)
return
}
httpClient := new(http.Client)
if err = userConfig.GetAccessToken(token, verifier, service, httpClient); err != nil {
log.Printf("Error getting access token: %v", err)
http.Error(rw, "Problem getting an access token", 500)
return
}
log.Printf("Ending session %v.\n", sessionID)
delete(sessions, sessionID)
http.SetCookie(rw, SessionEndCookie())
rw.Header().Set("Content-Type", "text/html;charset=utf-8")
fmt.Fprintf(rw, "<pre>")
fmt.Fprintf(rw, "Access Token: %v\n", userConfig.AccessTokenKey)
fmt.Fprintf(rw, "Token Secret: %v\n", userConfig.AccessTokenSecret)
fmt.Fprintf(rw, "Screen Name: %v\n", userConfig.AccessValues.Get("screen_name"))
fmt.Fprintf(rw, "User ID: %v\n", userConfig.AccessValues.Get("user_id"))
fmt.Fprintf(rw, "</pre>")
fmt.Fprintf(rw, "<a href=\"/signin\">Sign in again</a>")
}
type Settings struct {
Key string
Sec string
Port int
}
func main() {
sessions = map[string]*oauth1a.UserConfig{}
settings = &Settings{}
flag.IntVar(&settings.Port, "port", 10000, "Port to run on")
flag.StringVar(&settings.Key, "key", "", "Consumer key of your app")
flag.StringVar(&settings.Sec, "secret", "", "Consumer secret of your app")
flag.Parse()
if settings.Key == "" || settings.Sec == "" {
fmt.Fprintf(os.Stderr, "You must specify a consumer key and secret.\n")
flag.PrintDefaults()
os.Exit(1)
}
service = &oauth1a.Service{
RequestURL: "https://api.twitter.com/oauth/request_token",
AuthorizeURL: "https://api.twitter.com/oauth/authorize",
AccessURL: "https://api.twitter.com/oauth/access_token",
ClientConfig: &oauth1a.ClientConfig{
ConsumerKey: settings.Key,
ConsumerSecret: settings.Sec,
CallbackURL: "http://localhost:10000/callback/",
},
Signer: new(oauth1a.HmacSha1Signer),
}
http.HandleFunc("/", BaseHandler)
http.HandleFunc("/signin/", SignInHandler)
http.HandleFunc("/callback/", CallbackHandler)
log.Printf("Visit http://localhost:%v in your browser\n", settings.Port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", settings.Port), nil))
}