/
mangoauth_test.go
153 lines (122 loc) · 3.87 KB
/
mangoauth_test.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
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
package mangoauth
import (
"encoding/json"
"fmt"
. "github.com/paulbellamy/mango"
"github.com/sunfmin/goauth"
"github.com/sunfmin/integrationtest"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"strings"
)
var storageImpl = make(map[string]interface{})
type user struct {
id string
}
func (u *user) IdForSession() string {
return u.id
}
type urlp struct{}
type userp struct{}
type st struct{}
func (up *urlp) LoginURL(env Env) string {
return "/login"
}
func (up *urlp) OAuthReadyURL(env Env) string {
return "/oauthready"
}
func (up *urlp) SuccessURL(env Env) string {
return "/profile"
}
func (up *userp) LoadUser(id string) (u User, err error) {
u, _ = storageImpl["User:"+id].(User)
return
}
func (up *st) LoadTemporaryCredential(oauthToken string) (tc *goauth.TemporaryCredential, err error) {
tc, _ = storageImpl["TemporaryCredential:"+tc.OAuthToken].(*goauth.TemporaryCredential)
return
}
func (up *st) SaveTemporaryCredential(tc *goauth.TemporaryCredential) (err error) {
storageImpl["TemporaryCredential:"+tc.OAuthToken] = tc
return
}
func (up *st) SaveTokenCredential(env Env, tc *goauth.TokenCredential) (err error) {
storageImpl["Token:"+tc.UserId] = tc
storageImpl["User:"+tc.UserId] = &user{tc.UserId}
return
}
func (up *st) UserByToken(env Env, tc *goauth.TokenCredential) (u User) {
u = storageImpl["User:"+tc.UserId].(*user)
return
}
func (up *st) OAuthClient(env Env) (c *goauth.Client) {
cc := getClientKeyMap()
c = goauth.NewClient(cc["weibo"].Key, cc["weibo"].Secret, &goauth.Configuration{
RequestTokenURL: "http://api.t.sina.com.cn/oauth/request_token",
AccessTokenURL: "http://api.t.sina.com.cn/oauth/access_token",
AuthorizeURL: "http://api.t.sina.com.cn/oauth/authorize",
UseAuthorizationHeader: true,
UseBodyHash: true,
UserIdKey: "user_id",
})
return
}
func getClientKeyMap() (cc map[string]*goauth.ClientCredential) {
f, err := os.Open(os.Getenv("HOME") + "/.goauth.json")
if err != nil {
panic(err)
}
bs, _ := ioutil.ReadAll(f)
json.Unmarshal(bs, &cc)
return cc
}
func ExampleSetupOAuth() {
SetupOAuth("userkey", new(urlp), new(st), new(userp))
s := new(Stack)
session := Sessions("123123123213", "mangoauth", &CookieOptions{Path: "/", MaxAge: 3600 * 24 * 7})
s.Middleware(session)
m := http.NewServeMux()
m.HandleFunc("/login", s.HandlerFunc(OAuthLogin))
m.HandleFunc("/logout", s.HandlerFunc(Logout))
m.HandleFunc("/oauthready", s.HandlerFunc(OAuthReady))
restrictedStack := new(Stack)
restrictedStack.Middleware(session, MustLoggedIn)
profile := func(env Env) (status Status, headers Headers, body Body) {
return 200, Headers{}, Body("Hi, this is my profile")
}
m.HandleFunc("/profile", restrictedStack.HandlerFunc(profile))
fakelogin := func(env Env) (status Status, headers Headers, body Body) {
s := env.Session()
s["userkey"] = "user1"
return 200, Headers{}, Body("")
}
m.HandleFunc("/fakelogin", s.HandlerFunc(fakelogin))
ts := httptest.NewServer(m)
defer ts.Close()
is := integrationtest.NewSession()
is.Get(ts.URL + "/login")
for k, _ := range storageImpl {
fmt.Printf("have TemporaryCredential stored: %+v\n", strings.Contains(k, "TemporaryCredential"))
}
// logged in can access profile
is.Get(ts.URL + "/fakelogin")
storageImpl["User:user1"] = &user{"user1"}
r, _ := is.Get(ts.URL + "/profile")
b, _ := ioutil.ReadAll(r.Body)
if strings.Contains(string(b), "Hi, this is my profile") {
fmt.Println("can access profile when logged in")
}
// didn't log in can not access profile
delete(storageImpl, "User:user1")
r, _ = is.Get(ts.URL + "/profile")
b, _ = ioutil.ReadAll(r.Body)
if !strings.Contains(string(b), "Hi, this is my profile") {
fmt.Println("yes, can not access profile if not logged in")
}
//Output:
//have TemporaryCredential stored: true
//can access profile when logged in
//yes, can not access profile if not logged in
}