Repository navigation
Expand file tree
/
Copy pathaltAddress_test.go
More file actions
178 lines (156 loc) · 6.65 KB
/
Copy pathaltAddress_test.go
File metadata and controls
178 lines (156 loc) · 6.65 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"crypto/sha256"
"encoding/base64"
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/PuerkitoBio/goquery"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func sha256ForCode(s string) []byte {
h := sha256.Sum256([]byte(s))
return h[:]
}
func base64URLEncode(b []byte) string {
return base64.RawURLEncoding.EncodeToString(b)
}
func Test_oauthWithAltAddress(t *testing.T) {
app := &goBlog{
httpClient: newFakeHttpClient().Client,
cfg: createDefaultTestConfig(t),
}
app.cfg.Server.PublicAddress = "https://new.example.com"
app.cfg.Server.AltAddresses = []string{"https://old.example.com"}
app.cfg.Server.IndieAuthAddress = "https://old.example.com"
app.cfg.Blogs = map[string]*configBlog{
"en": {
Lang: "en",
},
}
app.cfg.User.Name = "John Doe"
app.cfg.User.Nick = "jdoe"
app.cfg.ActivityPub = &configActivityPub{Enabled: true}
app.cfg.Cache.Enable = false
err := app.initConfig(false)
require.NoError(t, err)
_ = app.initTemplateStrings()
app.reloadRouter()
t.Run("discover metadata from main domain returns alt address endpoints", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "https://new.example.com/.well-known/oauth-authorization-server", nil)
req.Host = "new.example.com"
rec := httptest.NewRecorder()
app.d.ServeHTTP(rec, req)
require.Equal(t, http.StatusOK, rec.Code)
var metadata map[string]any
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &metadata))
assert.Equal(t, "https://old.example.com/", metadata["issuer"])
assert.Equal(t, "https://old.example.com/oauth/authorize", metadata["authorization_endpoint"])
assert.Equal(t, "https://old.example.com/oauth/token", metadata["token_endpoint"])
})
t.Run("full authentication flow via alt address", func(t *testing.T) {
clientID := "https://client.example.com/"
redirectURI := "https://client.example.com/redirect"
scope := "create"
state := "test-state"
codeVerifier := "test-code-verifier-that-is-long-enough-for-sha256-validation-1234567890"
hash := sha256ForCode(codeVerifier)
codeChallenge := base64URLEncode(hash)
authorizeURL := "https://old.example.com/oauth/authorize?response_type=code" +
"&client_id=" + url.QueryEscape(clientID) +
"&redirect_uri=" + url.QueryEscape(redirectURI) +
"&scope=" + url.QueryEscape(scope) +
"&state=" + url.QueryEscape(state) +
"&code_challenge=" + url.QueryEscape(codeChallenge) +
"&code_challenge_method=S256"
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, authorizeURL, nil)
setLoggedIn(req, true)
app.d.ServeHTTP(rec, req)
require.Equal(t, http.StatusOK, rec.Code, "authorize page failed: %s", rec.Body.String())
parsedHtml, err := goquery.NewDocumentFromReader(strings.NewReader(rec.Body.String()))
require.NoError(t, err)
indieauthForm := parsedHtml.Find("form[action='/oauth/authorize']")
require.Equal(t, 1, indieauthForm.Length(), "form not found")
formRedirectUri := indieauthForm.Find("input[name='redirect_uri']").AttrOr("value", "")
assert.Equal(t, redirectURI, formRedirectUri)
formClientId := indieauthForm.Find("input[name='client_id']").AttrOr("value", "")
assert.Equal(t, clientID, formClientId)
formCodeChallenge := indieauthForm.Find("input[name='code_challenge']").AttrOr("value", "")
assert.NotEmpty(t, formCodeChallenge)
formCodeChallengeMethod := indieauthForm.Find("input[name='code_challenge_method']").AttrOr("value", "")
assert.Equal(t, "S256", formCodeChallengeMethod)
formState := indieauthForm.Find("input[name='state']").AttrOr("value", "")
assert.NotEmpty(t, formState)
rec = httptest.NewRecorder()
reqBody := url.Values{
"redirect_uri": {formRedirectUri},
"client_id": {formClientId},
"scope": {scope},
"code_challenge": {formCodeChallenge},
"code_challenge_method": {formCodeChallengeMethod},
"state": {formState},
}
req = httptest.NewRequest(http.MethodPost, "https://old.example.com/oauth/authorize?"+reqBody.Encode(), nil)
req.Host = "old.example.com"
setLoggedIn(req, true)
app.d.ServeHTTP(rec, req)
assert.Equal(t, http.StatusFound, rec.Code)
redirectLocation := rec.Header().Get("Location")
require.NotEmpty(t, redirectLocation)
redirectUrl, err := url.Parse(redirectLocation)
require.NoError(t, err)
code := redirectUrl.Query().Get("code")
require.NotEmpty(t, code)
assert.Equal(t, state, redirectUrl.Query().Get("state"))
// Verify iss parameter is the alt address; me is returned in the token response, not the redirect
assert.Equal(t, "https://old.example.com/", redirectUrl.Query().Get("iss"))
assert.Empty(t, redirectUrl.Query().Get("me"))
// Exchange code for token via alt address
tokenValues := url.Values{
"grant_type": {"authorization_code"},
"code": {code},
"redirect_uri": {redirectURI},
"client_id": {clientID},
"code_verifier": {codeVerifier},
}
rec = httptest.NewRecorder()
req = httptest.NewRequest(http.MethodPost, "https://old.example.com/oauth/token", strings.NewReader(tokenValues.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
app.d.ServeHTTP(rec, req)
require.Equal(t, http.StatusOK, rec.Code, "token exchange failed: %s", rec.Body.String())
var tokenResp map[string]any
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &tokenResp))
accessToken, ok := tokenResp["access_token"].(string)
require.True(t, ok)
require.NotEmpty(t, accessToken)
assert.Equal(t, "https://old.example.com/", tokenResp["me"])
// Verify token via alt address
rec = httptest.NewRecorder()
req = httptest.NewRequest(http.MethodGet, "https://old.example.com/oauth/token", nil)
req.Host = "old.example.com"
req.Header.Set("Authorization", "Bearer "+accessToken)
app.d.ServeHTTP(rec, req)
require.Equal(t, http.StatusOK, rec.Code)
var verifyResp map[string]any
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &verifyResp))
assert.Equal(t, true, verifyResp["active"])
assert.Equal(t, "https://old.example.com/", verifyResp["me"])
})
t.Run("html header has indieauth links to alt address", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "https://new.example.com/", nil)
req.Host = "new.example.com"
req.Header.Set("Accept", "text/html")
rec := httptest.NewRecorder()
app.d.ServeHTTP(rec, req)
body := rec.Body.String()
// Note: HTML may be minified with unquoted attributes
assert.Contains(t, body, "https://old.example.com/oauth/authorize")
assert.Contains(t, body, "https://old.example.com/oauth/token")
assert.Contains(t, body, "https://old.example.com/.well-known/oauth-authorization-server")
})
}