Skip to content

Commit 00565c8

Browse files
authored
Fix/cookie host (#76)
* fix: cookie host * feat: add test for url utils * fix: url test * fix: multi domain cookie if allowed
1 parent 74a551a commit 00565c8

File tree

5 files changed

+41
-4
lines changed

5 files changed

+41
-4
lines changed

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ VERSION := $(or $(VERSION),$(DEFAULT_VERSION))
44
cmd:
55
cd server && go build -ldflags "-w -X main.Version=$(VERSION)" -o '../build/server'
66
clean:
7-
rm -rf build
7+
rm -rf build
8+
test:
9+
cd server && go test ./...

server/handlers/oauthCallback.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func processGoogleUserInfo(code string) (db.User, error) {
4040
// Parse and verify ID Token payload.
4141
idToken, err := verifier.Verify(ctx, rawIDToken)
4242
if err != nil {
43-
return user, fmt.Errorf("unable to verify id_token:", err.Error())
43+
return user, fmt.Errorf("unable to verify id_token: %s", err.Error())
4444
}
4545

4646
// Extract custom claims

server/utils/cookie.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@ func SetCookie(gc *gin.Context, token string) {
1111
secure := true
1212
httpOnly := true
1313
host := GetHostName(constants.AUTHORIZER_URL)
14+
domain := GetDomainName(constants.AUTHORIZER_URL)
15+
if domain != "localhost" {
16+
domain = "." + domain
17+
}
1418

1519
gc.SetSameSite(http.SameSiteNoneMode)
1620
gc.SetCookie(constants.COOKIE_NAME, token, 3600, "/", host, secure, httpOnly)
21+
gc.SetCookie(constants.COOKIE_NAME+"-client", token, 3600, "/", domain, secure, httpOnly)
1722
}
1823

1924
func GetCookie(gc *gin.Context) (string, error) {
@@ -29,8 +34,13 @@ func DeleteCookie(gc *gin.Context) {
2934
secure := true
3035
httpOnly := true
3136

32-
host := GetHostName(constants.AUTHORIZER_URL)
37+
host := GetDomainName(constants.AUTHORIZER_URL)
38+
domain := GetDomainName(constants.AUTHORIZER_URL)
39+
if domain != "localhost" {
40+
domain = "." + domain
41+
}
3342

3443
gc.SetSameSite(http.SameSiteNoneMode)
3544
gc.SetCookie(constants.COOKIE_NAME, "", -1, "/", host, secure, httpOnly)
45+
gc.SetCookie(constants.COOKIE_NAME+"-client", "", -1, "/", domain, secure, httpOnly)
3646
}

server/utils/urls.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func GetHostName(auth_url string) string {
1717
return host
1818
}
1919

20-
// function to get domain name
20+
// GetDomainName function to get domain name
2121
func GetDomainName(auth_url string) string {
2222
u, err := url.Parse(auth_url)
2323
if err != nil {

server/utils/urls_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package utils
2+
3+
import "testing"
4+
5+
func TestGetHostName(t *testing.T) {
6+
authorizer_url := "http://test.herokuapp.com"
7+
8+
got := GetHostName(authorizer_url)
9+
want := "test.herokuapp.com"
10+
11+
if got != want {
12+
t.Errorf("GetHostName Test failed got %s, wanted %s", got, want)
13+
}
14+
}
15+
16+
func TestGetDomainName(t *testing.T) {
17+
authorizer_url := "http://test.herokuapp.com"
18+
19+
got := GetDomainName(authorizer_url)
20+
want := "herokuapp.com"
21+
22+
if got != want {
23+
t.Errorf("GetHostName Test failed got %q, wanted %q", got, want)
24+
}
25+
}

0 commit comments

Comments
 (0)