Files
rustdesk-api/model/oauth.go
T

148 lines
3.7 KiB
Go
Raw Permalink Normal View History

2024-09-19 10:44:49 +08:00
package model
2024-11-02 04:01:28 +08:00
import (
2024-11-05 09:48:02 +08:00
"errors"
2024-11-02 04:01:28 +08:00
"strconv"
2024-11-03 18:04:28 +08:00
"strings"
2024-11-02 04:01:28 +08:00
)
2024-11-03 18:04:28 +08:00
const OIDC_DEFAULT_SCOPES = "openid,profile,email"
2024-11-02 04:01:28 +08:00
const (
2024-11-04 21:30:58 +08:00
// make sure the value shouldbe lowercase
2024-11-02 04:01:28 +08:00
OauthTypeGithub string = "github"
OauthTypeGoogle string = "google"
OauthTypeOidc string = "oidc"
OauthTypeWebauth string = "webauth"
)
2024-11-04 21:30:58 +08:00
// 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")
}
}
2024-11-03 05:11:31 +08:00
const (
2024-11-05 09:48:02 +08:00
UserEndpointGithub string = "https://api.github.com/user"
IssuerGoogle string = "https://accounts.google.com"
2024-11-03 05:11:31 +08:00
)
2024-11-02 04:01:28 +08:00
2024-09-19 10:44:49 +08:00
type Oauth struct {
IdModel
2024-11-05 09:48:02 +08:00
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"`
2024-09-19 10:44:49 +08:00
TimeModel
}
2024-11-04 21:30:58 +08:00
// 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
}
2024-11-05 09:48:02 +08:00
switch oauthType {
case OauthTypeGithub:
oa.Op = OauthTypeGithub
case OauthTypeGoogle:
oa.Op = OauthTypeGoogle
}
2024-11-04 21:30:58 +08:00
// check if the op is empty, set the default value
op := strings.TrimSpace(oa.Op)
2024-11-05 09:48:02 +08:00
if op == "" && oauthType == OauthTypeOidc {
oa.Op = OauthTypeOidc
2024-11-04 21:30:58 +08:00
}
// 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)
2024-11-05 09:48:02 +08:00
// If the oauth type is google and the issuer is empty, set the issuer to the default value
2024-11-04 21:30:58 +08:00
if oauthType == OauthTypeGoogle && issuer == "" {
oa.Issuer = IssuerGoogle
}
return nil
}
2024-11-02 04:01:28 +08:00
type OauthUser struct {
2024-11-05 09:48:02 +08:00
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"`
Picture string `json:"picture,omitempty"`
2024-11-02 04:01:28 +08:00
}
func (ou *OauthUser) ToUser(user *User, overideUsername bool) {
if overideUsername {
user.Username = ou.Username
}
user.Email = ou.Email
user.Nickname = ou.Name
2024-11-03 05:33:59 +08:00
user.Avatar = ou.Picture
2024-11-02 04:01:28 +08:00
}
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"`
2024-11-03 18:04:28 +08:00
Picture string `json:"picture"`
2024-11-02 04:01:28 +08:00
}
func (ou *OidcUser) ToOauthUser() *OauthUser {
2024-11-03 18:04:28 +08:00
var username string
// 使用 PreferredUsername,如果不存在,降级到 Email 前缀
if ou.PreferredUsername != "" {
username = ou.PreferredUsername
} else {
2024-11-05 09:48:02 +08:00
username = strings.ToLower(ou.Email)
2024-11-03 18:04:28 +08:00
}
2024-11-02 04:01:28 +08:00
return &OauthUser{
2024-11-03 18:04:28 +08:00
OpenId: ou.Sub,
Name: ou.Name,
Username: username,
Email: ou.Email,
VerifiedEmail: ou.VerifiedEmail,
Picture: ou.Picture,
2024-11-02 04:01:28 +08:00
}
}
type GithubUser struct {
OauthUserBase
2024-11-05 09:48:02 +08:00
Id int `json:"id"`
Login string `json:"login"`
AvatarUrl string `json:"avatar_url"`
VerifiedEmail bool `json:"verified_email"`
2024-11-02 04:01:28 +08:00
}
func (gu *GithubUser) ToOauthUser() *OauthUser {
2024-11-03 18:04:28 +08:00
username := strings.ToLower(gu.Login)
2024-11-02 04:01:28 +08:00
return &OauthUser{
2024-11-05 09:48:02 +08:00
OpenId: strconv.Itoa(gu.Id),
Name: gu.Name,
Username: username,
Email: gu.Email,
VerifiedEmail: gu.VerifiedEmail,
Picture: gu.AvatarUrl,
2024-11-02 04:01:28 +08:00
}
}
2024-09-19 10:44:49 +08:00
type OauthList struct {
Oauths []*Oauth `json:"list"`
Pagination
}