This repository has been archived by the owner on Mar 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathauthorize_test.go
138 lines (120 loc) · 3.5 KB
/
authorize_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
package osindynamodb
import (
"net/http"
"net/url"
"testing"
"time"
"github.com/RangelReale/osin"
"github.com/stretchr/testify/assert"
)
func TestAuthorizeCode(t *testing.T) {
t.Parallel()
storageConfig := CreateStorageConfig("TestAuthorizeCode")
svc := createDynamoDB()
storage := New(svc, storageConfig)
err := storage.CreateSchema()
assert.Nil(t, err, "%s", err)
defer storage.DropSchema()
client := &osin.DefaultClient{
Id: "1234",
Secret: "aabbccdd",
RedirectUri: "/dev/null",
}
err = storage.CreateClient(client)
assert.Nil(t, err, "%s", err)
authorizeData := &osin.AuthorizeData{
Client: client,
Code: "9999",
ExpiresIn: 3600,
RedirectUri: "/dev/null",
CreatedAt: time.Now(),
}
err = storage.SaveAuthorize(authorizeData)
assert.Nil(t, err, "%s", err)
// -- -- --
sconfig := osin.NewServerConfig()
sconfig.AllowedAuthorizeTypes = osin.AllowedAuthorizeType{osin.CODE}
server := osin.NewServer(sconfig, storage)
server.AuthorizeTokenGen = &TestingAuthorizeTokenGen{}
resp := server.NewResponse()
req, err := http.NewRequest("GET", "http://localhost:14000/appauth", nil)
if err != nil {
t.Fatal(err)
}
req.Form = make(url.Values)
req.Form.Set("response_type", string(osin.CODE))
req.Form.Set("client_id", "1234")
req.Form.Set("state", "a")
if ar := server.HandleAuthorizeRequest(resp, req); ar != nil {
ar.Authorized = true
server.FinishAuthorizeRequest(resp, req, ar)
}
if resp.IsError && resp.InternalError != nil {
t.Fatalf("Error in response: %s", resp.InternalError)
}
if resp.IsError {
t.Fatalf("Should not be an error")
}
if resp.Type != osin.REDIRECT {
t.Fatalf("Response should be a redirect")
}
if d := resp.Output["code"]; d != "1" {
t.Fatalf("Unexpected authorization code: %s", d)
}
}
func TestAuthorizeToken(t *testing.T) {
t.Parallel()
storageConfig := CreateStorageConfig("TestAuthorizeToken")
svc := createDynamoDB()
storage := New(svc, storageConfig)
err := storage.CreateSchema()
assert.Nil(t, err, "%s", err)
defer storage.DropSchema()
client := &osin.DefaultClient{
Id: "1234",
Secret: "aabbccdd",
RedirectUri: "/dev/null",
}
err = storage.CreateClient(client)
assert.Nil(t, err, "%s", err)
authorizeData := &osin.AuthorizeData{
Client: client,
Code: "9999",
ExpiresIn: 3600,
RedirectUri: "/dev/null",
CreatedAt: time.Now(),
}
err = storage.SaveAuthorize(authorizeData)
assert.Nil(t, err, "%s", err)
// -- -- --
sconfig := osin.NewServerConfig()
sconfig.AllowedAuthorizeTypes = osin.AllowedAuthorizeType{osin.TOKEN}
server := osin.NewServer(sconfig, storage)
server.AuthorizeTokenGen = &TestingAuthorizeTokenGen{}
server.AccessTokenGen = &TestingAccessTokenGen{}
resp := server.NewResponse()
req, err := http.NewRequest("GET", "http://localhost:14000/appauth", nil)
if err != nil {
t.Fatal(err)
}
req.Form = make(url.Values)
req.Form.Set("response_type", string(osin.TOKEN))
req.Form.Set("client_id", "1234")
req.Form.Set("state", "a")
if ar := server.HandleAuthorizeRequest(resp, req); ar != nil {
ar.Authorized = true
server.FinishAuthorizeRequest(resp, req, ar)
}
if resp.IsError && resp.InternalError != nil {
t.Fatalf("Error in response: %s", resp.InternalError)
}
if resp.IsError {
t.Fatalf("Should not be an error")
}
if resp.Type != osin.REDIRECT || !resp.RedirectInFragment {
t.Fatalf("Response should be a redirect with fragment")
}
if d := resp.Output["access_token"]; d != "1" {
t.Fatalf("Unexpected access token: %s", d)
}
}