auth
2014-07-18
auth/storage_test.go
Start rewriting the repo. This code originally was a carbon copy of https://github.com/RangelReale/osin, but I am methodically stripping out the extensible nature of it for a simpler interface, while simultaneously bringing the style into line with the Ducky style.
1 package oauth2
3 import (
4 "errors"
5 "strconv"
6 "time"
7 )
9 type TestingStorage struct {
10 clients map[string]*Client
11 authorize map[string]*AuthorizeData
12 access map[string]*AccessData
13 refresh map[string]string
14 }
16 func NewTestingStorage() *TestingStorage {
17 r := &TestingStorage{
18 clients: make(map[string]*Client),
19 authorize: make(map[string]*AuthorizeData),
20 access: make(map[string]*AccessData),
21 refresh: make(map[string]string),
22 }
24 r.clients["1234"] = &Client{
25 Id: "1234",
26 Secret: "aabbccdd",
27 RedirectUri: "http://localhost:14000/appauth",
28 }
30 r.authorize["9999"] = &AuthorizeData{
31 Client: r.clients["1234"],
32 Code: "9999",
33 ExpiresIn: 3600,
34 CreatedAt: time.Now(),
35 RedirectUri: "http://localhost:14000/appauth",
36 }
38 r.access["9999"] = &AccessData{
39 Client: r.clients["1234"],
40 AuthorizeData: r.authorize["9999"],
41 AccessToken: "9999",
42 ExpiresIn: 3600,
43 CreatedAt: time.Now(),
44 }
46 r.access["r9999"] = &AccessData{
47 Client: r.clients["1234"],
48 AuthorizeData: r.authorize["9999"],
49 AccessData: r.access["9999"],
50 AccessToken: "9999",
51 RefreshToken: "r9999",
52 ExpiresIn: 3600,
53 CreatedAt: time.Now(),
54 }
56 r.refresh["r9999"] = "9999"
58 return r
59 }
61 func (s *TestingStorage) GetClient(id string) (*Client, error) {
62 if c, ok := s.clients[id]; ok {
63 return c, nil
64 }
65 return nil, errors.New("Client not found")
66 }
68 func (s *TestingStorage) SetClient(id string, client *Client) error {
69 s.clients[id] = client
70 return nil
71 }
73 func (s *TestingStorage) SaveAuthorize(data *AuthorizeData) error {
74 s.authorize[data.Code] = data
75 return nil
76 }
78 func (s *TestingStorage) LoadAuthorize(code string) (*AuthorizeData, error) {
79 if d, ok := s.authorize[code]; ok {
80 return d, nil
81 }
82 return nil, errors.New("Authorize not found")
83 }
85 func (s *TestingStorage) RemoveAuthorize(code string) error {
86 delete(s.authorize, code)
87 return nil
88 }
90 func (s *TestingStorage) SaveAccess(data *AccessData) error {
91 s.access[data.AccessToken] = data
92 if data.RefreshToken != "" {
93 s.refresh[data.RefreshToken] = data.AccessToken
94 }
95 return nil
96 }
98 func (s *TestingStorage) LoadAccess(code string) (*AccessData, error) {
99 if d, ok := s.access[code]; ok {
100 return d, nil
101 }
102 return nil, errors.New("Access not found")
103 }
105 func (s *TestingStorage) RemoveAccess(code string) error {
106 delete(s.access, code)
107 return nil
108 }
110 func (s *TestingStorage) LoadRefresh(code string) (*AccessData, error) {
111 if d, ok := s.refresh[code]; ok {
112 return s.LoadAccess(d)
113 }
114 return nil, errors.New("Refresh not found")
115 }
117 func (s *TestingStorage) RemoveRefresh(code string) error {
118 delete(s.refresh, code)
119 return nil
120 }
122 // Predictable testing token generation
124 type TestingAuthorizeTokenGen struct {
125 counter int64
126 }
128 func (a *TestingAuthorizeTokenGen) GenerateAuthorizeToken(data *AuthorizeData) (ret string, err error) {
129 a.counter++
130 return strconv.FormatInt(a.counter, 10), nil
131 }
133 type TestingAccessTokenGen struct {
134 acounter int64
135 rcounter int64
136 }
138 func (a *TestingAccessTokenGen) GenerateAccessToken(data *AccessData, generaterefresh bool) (accesstoken string, refreshtoken string, err error) {
139 a.acounter++
140 accesstoken = strconv.FormatInt(a.acounter, 10)
142 if generaterefresh {
143 a.rcounter++
144 refreshtoken = "r" + strconv.FormatInt(a.rcounter, 10)
145 }
146 return
147 }