Skip to content

Commit 80121f2

Browse files
committed
fix: review comments
1 parent eef674a commit 80121f2

2 files changed

Lines changed: 7 additions & 20 deletions

File tree

internal/service/github_oauth_service.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"io"
1111
"net/http"
12+
"strconv"
1213
"time"
1314

1415
"github.com/steveiliop56/tinyauth/internal/config"
@@ -27,6 +28,7 @@ type GithubEmailResponse []struct {
2728
type GithubUserInfoResponse struct {
2829
Login string `json:"login"`
2930
Name string `json:"name"`
31+
ID int `json:"id"`
3032
}
3133

3234
type GithubOAuthService struct {
@@ -172,9 +174,7 @@ func (github *GithubOAuthService) Userinfo() (config.Claims, error) {
172174

173175
user.PreferredUsername = userInfo.Login
174176
user.Name = userInfo.Name
175-
176-
// Github does not implement OIDC, so no sub is available
177-
user.Sub = "not_available_dont_use_me"
177+
user.Sub = strconv.Itoa(userInfo.ID)
178178

179179
return user, nil
180180
}

internal/service/google_oauth_service.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,7 @@ import (
1717
"golang.org/x/oauth2/endpoints"
1818
)
1919

20-
var GoogleOAuthScopes = []string{"https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile"}
21-
22-
type GoogleUserInfoResponse struct {
23-
Email string `json:"email"`
24-
Name string `json:"name"`
25-
Id string `json:"id"`
26-
}
20+
var GoogleOAuthScopes = []string{"openid", "email", "profile"}
2721

2822
type GoogleOAuthService struct {
2923
config oauth2.Config
@@ -92,7 +86,7 @@ func (google *GoogleOAuthService) Userinfo() (config.Claims, error) {
9286

9387
client := google.config.Client(google.context, google.token)
9488

95-
res, err := client.Get("https://www.googleapis.com/userinfo/v2/me")
89+
res, err := client.Get("https://openidconnect.googleapis.com/v1/userinfo")
9690
if err != nil {
9791
return config.Claims{}, err
9892
}
@@ -107,19 +101,12 @@ func (google *GoogleOAuthService) Userinfo() (config.Claims, error) {
107101
return config.Claims{}, err
108102
}
109103

110-
var userInfo GoogleUserInfoResponse
111-
112-
err = json.Unmarshal(body, &userInfo)
104+
err = json.Unmarshal(body, &user)
113105
if err != nil {
114106
return config.Claims{}, err
115107
}
116108

117-
user.PreferredUsername = strings.Split(userInfo.Email, "@")[0]
118-
user.Name = userInfo.Name
119-
user.Email = userInfo.Email
120-
121-
// We can use the id as the sub
122-
user.Sub = userInfo.Id
109+
user.PreferredUsername = strings.SplitN(user.Email, "@", 2)[0]
123110

124111
return user, nil
125112
}

0 commit comments

Comments
 (0)