re-construct oauth

This commit is contained in:
Tao Chen
2024-11-02 04:01:28 +08:00
parent c5e3482538
commit 485ae54e9e
14 changed files with 491 additions and 491 deletions

View File

@@ -1,23 +1,110 @@
package model
import (
"strconv"
"fmt"
)
const (
OauthTypeGithub string = "github"
OauthTypeGoogle string = "google"
OauthTypeOidc string = "oidc"
OauthTypeWebauth string = "webauth"
)
type Oauth struct {
IdModel
Op string `json:"op"`
ClientId string `json:"client_id"`
ClientSecret string `json:"client_secret"`
RedirectUrl string `json:"redirect_url"`
AutoRegister *bool `json:"auto_register"`
Scopes string `json:"scopes"`
Issuer string `json:"issuer"`
Op string `json:"op"`
OauthType string `json:"oauth_type"`
ClientId string `json:"client_id"`
ClientSecret string `json:"client_secret"`
RedirectUrl string `json:"redirect_url"`
AutoRegister *bool `json:"auto_register"`
Scopes string `json:"scopes"`
Issuer string `json:"issuer"`
TimeModel
}
const (
OauthTypeGithub = "github"
OauthTypeGoogle = "google"
OauthTypeOidc = "oidc"
OauthTypeWebauth = "webauth"
)
type OauthUser struct {
OpenId string `json:"open_id" gorm:"not null;index"`
Name string `json:"name"`
Username string `json:"username"`
Email string `json:"email"`
VerifiedEmail bool `json:"verified_email,omitempty"`
}
func (ou *OauthUser) ToUser(user *User, overideUsername bool) {
if overideUsername {
user.Username = ou.Username
}
user.Email = ou.Email
user.Nickname = ou.Name
}
type OauthUserBase struct {
Name string `json:"name"`
Email string `json:"email"`
}
type OidcUser struct {
OauthUserBase
Sub string `json:"sub"`
VerifiedEmail bool `json:"email_verified"`
PreferredUsername string `json:"preferred_username"`
}
func (ou *OidcUser) ToOauthUser() *OauthUser {
return &OauthUser{
OpenId: ou.Sub,
Name: ou.Name,
Username: ou.PreferredUsername,
Email: ou.Email,
VerifiedEmail: ou.VerifiedEmail,
}
}
type GoogleUser struct {
OauthUserBase
FamilyName string `json:"family_name"`
GivenName string `json:"given_name"`
Id string `json:"id"`
Picture string `json:"picture"`
VerifiedEmail bool `json:"verified_email"`
}
func (gu *GoogleUser) ToOauthUser() *OauthUser {
return &OauthUser{
OpenId: gu.Id,
Name: fmt.Sprintf("%s %s", gu.GivenName, gu.FamilyName),
Username: gu.GivenName,
Email: gu.Email,
VerifiedEmail: gu.VerifiedEmail,
}
}
type GithubUser struct {
OauthUserBase
Id int `json:"id"`
Login string `json:"login"`
}
func (gu *GithubUser) ToOauthUser() *OauthUser {
return &OauthUser{
OpenId: strconv.Itoa(gu.Id),
Name: gu.Name,
Username: gu.Login,
Email: gu.Email,
VerifiedEmail: true,
}
}
type OauthList struct {
Oauths []*Oauth `json:"list"`

View File

@@ -1,8 +1,15 @@
package model
import (
"fmt"
"gorm.io/gorm"
)
type User struct {
IdModel
Username string `json:"username" gorm:"default:'';not null;uniqueIndex"`
Email string `json:"email" gorm:"default:'';not null;uniqueIndex"`
// Email string `json:"email" `
Password string `json:"-" gorm:"default:'';not null;"`
Nickname string `json:"nickname" gorm:"default:'';not null;"`
Avatar string `json:"avatar" gorm:"default:'';not null;"`
@@ -12,6 +19,15 @@ type User struct {
TimeModel
}
// BeforeSave 钩子用于确保 email 字段有合理的默认值
func (u *User) BeforeSave(tx *gorm.DB) (err error) {
// 如果 email 为空,设置为默认值
if u.Email == "" {
u.Email = fmt.Sprintf("%s@example.com", u.Username)
}
return nil
}
type UserList struct {
Users []*User `json:"list,omitempty"`
Pagination

View File

@@ -2,11 +2,18 @@ package model
type UserThird struct {
IdModel
UserId uint `json:"user_id" gorm:"not null;index"`
OpenId string `json:"open_id" gorm:"not null;index"`
UnionId string `json:"union_id" gorm:"not null;"`
ThirdType string `json:"third_type" gorm:"not null;"`
ThirdEmail string `json:"third_email"`
ThirdName string `json:"third_name"`
UserId uint ` json:"user_id" gorm:"not null;index"`
OauthUser
// UnionId string `json:"union_id" gorm:"not null;"`
// OauthType string `json:"oauth_type" gorm:"not null;"`
OauthType string `json:"oauth_type"`
Op string `json:"op" gorm:"not null;"`
TimeModel
}
func (u *UserThird) FromOauthUser(userId uint, oauthUser *OauthUser, oauthType string, op string) {
u.UserId = userId
u.OauthUser = *oauthUser
u.OauthType = oauthType
u.Op = op
}