fix google
This commit is contained in:
@@ -2,9 +2,10 @@ package model
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"fmt"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const OIDC_DEFAULT_SCOPES = "openid,profile,email"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
OauthTypeGithub string = "github"
|
OauthTypeGithub string = "github"
|
||||||
@@ -57,50 +58,45 @@ func (ou *OauthUser) ToUser(user *User, overideUsername bool) {
|
|||||||
user.Avatar = ou.Picture
|
user.Avatar = ou.Picture
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
type OauthUserBase struct {
|
type OauthUserBase struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
type OidcUser struct {
|
type OidcUser struct {
|
||||||
OauthUserBase
|
OauthUserBase
|
||||||
Sub string `json:"sub"`
|
Sub string `json:"sub"`
|
||||||
VerifiedEmail bool `json:"email_verified"`
|
VerifiedEmail bool `json:"email_verified"`
|
||||||
PreferredUsername string `json:"preferred_username"`
|
PreferredUsername string `json:"preferred_username"`
|
||||||
Picture string `json:"picture"`
|
Picture string `json:"picture"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ou *OidcUser) ToOauthUser() *OauthUser {
|
func (ou *OidcUser) ToOauthUser() *OauthUser {
|
||||||
|
var username string
|
||||||
|
// 使用 PreferredUsername,如果不存在,降级到 Email 前缀
|
||||||
|
if ou.PreferredUsername != "" {
|
||||||
|
username = ou.PreferredUsername
|
||||||
|
} else {
|
||||||
|
username = strings.ToLower(strings.Split(ou.Email, "@")[0])
|
||||||
|
}
|
||||||
|
|
||||||
return &OauthUser{
|
return &OauthUser{
|
||||||
OpenId: ou.Sub,
|
OpenId: ou.Sub,
|
||||||
Name: ou.Name,
|
Name: ou.Name,
|
||||||
Username: ou.PreferredUsername,
|
Username: username,
|
||||||
Email: ou.Email,
|
Email: ou.Email,
|
||||||
VerifiedEmail: ou.VerifiedEmail,
|
VerifiedEmail: ou.VerifiedEmail,
|
||||||
Picture: ou.Picture,
|
Picture: ou.Picture,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type GoogleUser struct {
|
type GoogleUser struct {
|
||||||
OauthUserBase
|
OidcUser
|
||||||
FamilyName string `json:"family_name"`
|
|
||||||
GivenName string `json:"given_name"`
|
|
||||||
Id string `json:"id"`
|
|
||||||
Picture string `json:"picture"`
|
|
||||||
VerifiedEmail bool `json:"verified_email"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GoogleUser 使用特定的 Username 规则来调用 ToOauthUser
|
||||||
func (gu *GoogleUser) ToOauthUser() *OauthUser {
|
func (gu *GoogleUser) ToOauthUser() *OauthUser {
|
||||||
return &OauthUser{
|
return gu.OidcUser.ToOauthUser()
|
||||||
OpenId: gu.Id,
|
|
||||||
Name: fmt.Sprintf("%s %s", gu.GivenName, gu.FamilyName),
|
|
||||||
Username: gu.GivenName,
|
|
||||||
Email: gu.Email,
|
|
||||||
VerifiedEmail: gu.VerifiedEmail,
|
|
||||||
Picture: gu.Picture,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -113,10 +109,11 @@ type GithubUser struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (gu *GithubUser) ToOauthUser() *OauthUser {
|
func (gu *GithubUser) ToOauthUser() *OauthUser {
|
||||||
|
username := strings.ToLower(gu.Login)
|
||||||
return &OauthUser{
|
return &OauthUser{
|
||||||
OpenId: strconv.Itoa(gu.Id),
|
OpenId: strconv.Itoa(gu.Id),
|
||||||
Name: gu.Name,
|
Name: gu.Name,
|
||||||
Username: gu.Login,
|
Username: username,
|
||||||
Email: gu.Email,
|
Email: gu.Email,
|
||||||
VerifiedEmail: gu.VerifiedEmail,
|
VerifiedEmail: gu.VerifiedEmail,
|
||||||
Picture: gu.AvatarUrl,
|
Picture: gu.AvatarUrl,
|
||||||
|
|||||||
@@ -170,7 +170,7 @@ func (os *OauthService) GetOauthConfig(op string) (err error, oauthInfo *model.O
|
|||||||
oauthConfig.Scopes = []string{"read:user", "user:email"}
|
oauthConfig.Scopes = []string{"read:user", "user:email"}
|
||||||
case model.OauthTypeGoogle:
|
case model.OauthTypeGoogle:
|
||||||
oauthConfig.Endpoint = google.Endpoint
|
oauthConfig.Endpoint = google.Endpoint
|
||||||
oauthConfig.Scopes = []string{"https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/userinfo.email"}
|
oauthConfig.Scopes = os.constructScopes(model.OIDC_DEFAULT_SCOPES)
|
||||||
case model.OauthTypeOidc:
|
case model.OauthTypeOidc:
|
||||||
var endpoint OidcEndpoint
|
var endpoint OidcEndpoint
|
||||||
err, endpoint = os.FetchOidcEndpoint(oauthInfo.Issuer)
|
err, endpoint = os.FetchOidcEndpoint(oauthInfo.Issuer)
|
||||||
@@ -374,7 +374,7 @@ func (os *OauthService) getScopesByOp(op string) []string {
|
|||||||
func (os *OauthService) constructScopes(scopes string) []string {
|
func (os *OauthService) constructScopes(scopes string) []string {
|
||||||
scopes = strings.TrimSpace(scopes)
|
scopes = strings.TrimSpace(scopes)
|
||||||
if scopes == "" {
|
if scopes == "" {
|
||||||
scopes = "openid,profile,email"
|
scopes = model.OIDC_DEFAULT_SCOPES
|
||||||
}
|
}
|
||||||
return strings.Split(scopes, ",")
|
return strings.Split(scopes, ",")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user