-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauth_test.go
133 lines (115 loc) · 3.22 KB
/
auth_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
package common
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/require"
)
func ExampleBasicAuth() {
fmt.Println(BasicAuth("username", "password"))
// Output: dXNlcm5hbWU6cGFzc3dvcmQ=
}
func TestDecodeBasicAuth(t *testing.T) {
out, err := Base64decode("dXNlcm5hbWU6cGFzc3dvcmQ=")
require.NoError(t, err)
require.Equal(t, "username:password", out)
}
func TestNewClient(t *testing.T) {
secret := "secret"
srv := mockSrv(secret)
t.Cleanup(func() {
srv.Close()
})
ctx := context.Background()
c := NewClient(ctx, &ClientConfiguration{
OAuth2: OAuth2{
ClientID: "clientid",
TokenURL: fmt.Sprintf("%s/oauth2/token", srv.URL),
Scopes: []string{"openid", "email", "groups"},
EndpointParams: url.Values{
"groups": []string{"test"},
},
ClientSecret: secret,
},
})
req, err := http.NewRequest("GET", fmt.Sprintf("%s?foo=bar", srv.URL), nil)
require.NoError(t, err)
resp, err := c.Do(req)
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
}
func TestNewClientToken(t *testing.T) {
secret := "tokenfile"
srv := mockSrv(secret)
t.Cleanup(func() {
srv.Close()
})
ctx := context.Background()
c := NewClient(ctx, &ClientConfiguration{
OAuth2: OAuth2{
ClientID: "clientid",
TokenURL: fmt.Sprintf("%s/oauth2/token", srv.URL),
Scopes: []string{"openid", "email", "groups"},
EndpointParams: url.Values{
"groups": []string{"test"},
},
ClientSecretFile: "./testdata/token",
},
})
for i := 0; i < 3; i++ {
req, err := http.NewRequest("GET", fmt.Sprintf("%s?foo=bar", srv.URL), nil)
require.NoError(t, err)
resp, err := c.Do(req)
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
time.Sleep(1 * time.Second)
}
}
type tokenRequest struct {
GrantType string `form:"grant_type" json:"grant_type"`
Scope string `form:"scope" json:"scope"`
ClientID string `form:"client_id" json:"client_id"`
ClientSecret string `form:"client_secret" json:"client_secret"`
}
func mockSrv(secret string) *httptest.Server {
r := gin.New()
r.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "ok")
})
r.POST("/oauth2/token", func(c *gin.Context) {
var payload tokenRequest
err := c.Bind(&payload)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if payload.GrantType != "client_credentials" {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid grant_type"})
return
}
requestedGroups := c.Request.Form["groups"]
if len(requestedGroups) != 1 || requestedGroups[0] != "test" {
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("invalid groups: %+v", requestedGroups)})
return
}
if payload.Scope != "openid email groups" {
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("invalid scope: %s", payload.Scope)})
return
}
if payload.ClientID != "clientid" || payload.ClientSecret != secret {
c.JSON(http.StatusUnauthorized, gin.H{"error": fmt.Sprintf("invalid client: %+v", payload)})
return
}
c.JSON(http.StatusOK, gin.H{
"access_token": "token",
"token_type": "Bearer",
"expires_in": 1,
})
})
return httptest.NewServer(r)
}