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.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/storage_test.go Fri Jul 18 07:13:22 2014 -0400 1.3 @@ -0,0 +1,147 @@ 1.4 +package oauth2 1.5 + 1.6 +import ( 1.7 + "errors" 1.8 + "strconv" 1.9 + "time" 1.10 +) 1.11 + 1.12 +type TestingStorage struct { 1.13 + clients map[string]*Client 1.14 + authorize map[string]*AuthorizeData 1.15 + access map[string]*AccessData 1.16 + refresh map[string]string 1.17 +} 1.18 + 1.19 +func NewTestingStorage() *TestingStorage { 1.20 + r := &TestingStorage{ 1.21 + clients: make(map[string]*Client), 1.22 + authorize: make(map[string]*AuthorizeData), 1.23 + access: make(map[string]*AccessData), 1.24 + refresh: make(map[string]string), 1.25 + } 1.26 + 1.27 + r.clients["1234"] = &Client{ 1.28 + Id: "1234", 1.29 + Secret: "aabbccdd", 1.30 + RedirectUri: "http://localhost:14000/appauth", 1.31 + } 1.32 + 1.33 + r.authorize["9999"] = &AuthorizeData{ 1.34 + Client: r.clients["1234"], 1.35 + Code: "9999", 1.36 + ExpiresIn: 3600, 1.37 + CreatedAt: time.Now(), 1.38 + RedirectUri: "http://localhost:14000/appauth", 1.39 + } 1.40 + 1.41 + r.access["9999"] = &AccessData{ 1.42 + Client: r.clients["1234"], 1.43 + AuthorizeData: r.authorize["9999"], 1.44 + AccessToken: "9999", 1.45 + ExpiresIn: 3600, 1.46 + CreatedAt: time.Now(), 1.47 + } 1.48 + 1.49 + r.access["r9999"] = &AccessData{ 1.50 + Client: r.clients["1234"], 1.51 + AuthorizeData: r.authorize["9999"], 1.52 + AccessData: r.access["9999"], 1.53 + AccessToken: "9999", 1.54 + RefreshToken: "r9999", 1.55 + ExpiresIn: 3600, 1.56 + CreatedAt: time.Now(), 1.57 + } 1.58 + 1.59 + r.refresh["r9999"] = "9999" 1.60 + 1.61 + return r 1.62 +} 1.63 + 1.64 +func (s *TestingStorage) GetClient(id string) (*Client, error) { 1.65 + if c, ok := s.clients[id]; ok { 1.66 + return c, nil 1.67 + } 1.68 + return nil, errors.New("Client not found") 1.69 +} 1.70 + 1.71 +func (s *TestingStorage) SetClient(id string, client *Client) error { 1.72 + s.clients[id] = client 1.73 + return nil 1.74 +} 1.75 + 1.76 +func (s *TestingStorage) SaveAuthorize(data *AuthorizeData) error { 1.77 + s.authorize[data.Code] = data 1.78 + return nil 1.79 +} 1.80 + 1.81 +func (s *TestingStorage) LoadAuthorize(code string) (*AuthorizeData, error) { 1.82 + if d, ok := s.authorize[code]; ok { 1.83 + return d, nil 1.84 + } 1.85 + return nil, errors.New("Authorize not found") 1.86 +} 1.87 + 1.88 +func (s *TestingStorage) RemoveAuthorize(code string) error { 1.89 + delete(s.authorize, code) 1.90 + return nil 1.91 +} 1.92 + 1.93 +func (s *TestingStorage) SaveAccess(data *AccessData) error { 1.94 + s.access[data.AccessToken] = data 1.95 + if data.RefreshToken != "" { 1.96 + s.refresh[data.RefreshToken] = data.AccessToken 1.97 + } 1.98 + return nil 1.99 +} 1.100 + 1.101 +func (s *TestingStorage) LoadAccess(code string) (*AccessData, error) { 1.102 + if d, ok := s.access[code]; ok { 1.103 + return d, nil 1.104 + } 1.105 + return nil, errors.New("Access not found") 1.106 +} 1.107 + 1.108 +func (s *TestingStorage) RemoveAccess(code string) error { 1.109 + delete(s.access, code) 1.110 + return nil 1.111 +} 1.112 + 1.113 +func (s *TestingStorage) LoadRefresh(code string) (*AccessData, error) { 1.114 + if d, ok := s.refresh[code]; ok { 1.115 + return s.LoadAccess(d) 1.116 + } 1.117 + return nil, errors.New("Refresh not found") 1.118 +} 1.119 + 1.120 +func (s *TestingStorage) RemoveRefresh(code string) error { 1.121 + delete(s.refresh, code) 1.122 + return nil 1.123 +} 1.124 + 1.125 +// Predictable testing token generation 1.126 + 1.127 +type TestingAuthorizeTokenGen struct { 1.128 + counter int64 1.129 +} 1.130 + 1.131 +func (a *TestingAuthorizeTokenGen) GenerateAuthorizeToken(data *AuthorizeData) (ret string, err error) { 1.132 + a.counter++ 1.133 + return strconv.FormatInt(a.counter, 10), nil 1.134 +} 1.135 + 1.136 +type TestingAccessTokenGen struct { 1.137 + acounter int64 1.138 + rcounter int64 1.139 +} 1.140 + 1.141 +func (a *TestingAccessTokenGen) GenerateAccessToken(data *AccessData, generaterefresh bool) (accesstoken string, refreshtoken string, err error) { 1.142 + a.acounter++ 1.143 + accesstoken = strconv.FormatInt(a.acounter, 10) 1.144 + 1.145 + if generaterefresh { 1.146 + a.rcounter++ 1.147 + refreshtoken = "r" + strconv.FormatInt(a.rcounter, 10) 1.148 + } 1.149 + return 1.150 +}