modify google ro re-use oidc

This commit is contained in:
Tao Chen
2024-11-04 21:30:58 +08:00
parent 5a53f180e4
commit 3acfb36c5d
3 changed files with 59 additions and 55 deletions

View File

@@ -3,17 +3,29 @@ package model
import (
"strconv"
"strings"
"errors"
)
const OIDC_DEFAULT_SCOPES = "openid,profile,email"
const (
// make sure the value shouldbe lowercase
OauthTypeGithub string = "github"
OauthTypeGoogle string = "google"
OauthTypeOidc string = "oidc"
OauthTypeWebauth string = "webauth"
)
// Validate the oauth type
func ValidateOauthType(oauthType string) error {
switch oauthType {
case OauthTypeGithub, OauthTypeGoogle, OauthTypeOidc, OauthTypeWebauth:
return nil
default:
return errors.New("invalid Oauth type")
}
}
const (
OauthNameGithub string = "GitHub"
OauthNameGoogle string = "Google"
@@ -23,8 +35,7 @@ const (
const (
UserEndpointGithub string = "https://api.github.com/user"
UserEndpointGoogle string = "https://www.googleapis.com/oauth2/v3/userinfo"
UserEndpointOidc string = ""
IssuerGoogle string = "https://accounts.google.com"
)
type Oauth struct {
@@ -40,6 +51,40 @@ type Oauth struct {
TimeModel
}
// Helper function to format oauth info, it's used in the update and create method
func (oa *Oauth) FormatOauthInfo() error {
oauthType := strings.TrimSpace(oa.OauthType)
err := ValidateOauthType(oa.OauthType)
if err != nil {
return err
}
// check if the op is empty, set the default value
op := strings.TrimSpace(oa.Op)
if op == "" {
switch oauthType {
case OauthTypeGithub:
oa.Op = OauthNameGithub
case OauthTypeGoogle:
oa.Op = OauthNameGoogle
case OauthTypeOidc:
oa.Op = OauthNameOidc
case OauthTypeWebauth:
oa.Op = OauthNameWebauth
default:
oa.Op = oauthType
}
}
// check the issuer, if the oauth type is google and the issuer is empty, set the issuer to the default value
issuer := strings.TrimSpace(oa.Issuer)
// If the oauth type is google and the issuer is empty, set the issuer to the default value
if oauthType == OauthTypeGoogle && issuer == "" {
oa.Issuer = IssuerGoogle
}
return nil
}
type OauthUser struct {
OpenId string `json:"open_id" gorm:"not null;index"`
Name string `json:"name"`
@@ -90,15 +135,6 @@ func (ou *OidcUser) ToOauthUser() *OauthUser {
}
}
type GoogleUser struct {
OidcUser
}
// GoogleUser 使用特定的 Username 规则来调用 ToOauthUser
func (gu *GoogleUser) ToOauthUser() *OauthUser {
return gu.OidcUser.ToOauthUser()
}
type GithubUser struct {
OauthUserBase