oauth.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package model
  2. import (
  3. "errors"
  4. "strconv"
  5. "strings"
  6. )
  7. const OIDC_DEFAULT_SCOPES = "openid,profile,email"
  8. const (
  9. // make sure the value shouldbe lowercase
  10. OauthTypeGithub string = "github"
  11. OauthTypeGoogle string = "google"
  12. OauthTypeOidc string = "oidc"
  13. OauthTypeWebauth string = "webauth"
  14. OauthTypeLinuxdo string = "linuxdo"
  15. PKCEMethodS256 string = "S256"
  16. PKCEMethodPlain string = "plain"
  17. )
  18. // Validate the oauth type
  19. func ValidateOauthType(oauthType string) error {
  20. switch oauthType {
  21. case OauthTypeGithub, OauthTypeGoogle, OauthTypeOidc, OauthTypeWebauth, OauthTypeLinuxdo:
  22. return nil
  23. default:
  24. return errors.New("invalid Oauth type")
  25. }
  26. }
  27. const (
  28. UserEndpointGithub string = "https://api.github.com/user"
  29. UserEndpointLinuxdo string = "https://connect.linux.do/api/user"
  30. IssuerGoogle string = "https://accounts.google.com"
  31. )
  32. type Oauth struct {
  33. IdModel
  34. Op string `json:"op"`
  35. OauthType string `json:"oauth_type"`
  36. ClientId string `json:"client_id"`
  37. ClientSecret string `json:"client_secret"`
  38. AutoRegister *bool `json:"auto_register"`
  39. Scopes string `json:"scopes"`
  40. Issuer string `json:"issuer"`
  41. PkceEnable *bool `json:"pkce_enable"`
  42. PkceMethod string `json:"pkce_method"`
  43. TimeModel
  44. }
  45. // Helper function to format oauth info, it's used in the update and create method
  46. func (oa *Oauth) FormatOauthInfo() error {
  47. oauthType := strings.TrimSpace(oa.OauthType)
  48. err := ValidateOauthType(oa.OauthType)
  49. if err != nil {
  50. return err
  51. }
  52. switch oauthType {
  53. case OauthTypeGithub:
  54. oa.Op = OauthTypeGithub
  55. case OauthTypeGoogle:
  56. oa.Op = OauthTypeGoogle
  57. case OauthTypeLinuxdo:
  58. oa.Op = OauthTypeLinuxdo
  59. }
  60. // check if the op is empty, set the default value
  61. op := strings.TrimSpace(oa.Op)
  62. if op == "" && oauthType == OauthTypeOidc {
  63. oa.Op = OauthTypeOidc
  64. }
  65. // check the issuer, if the oauth type is google and the issuer is empty, set the issuer to the default value
  66. issuer := strings.TrimSpace(oa.Issuer)
  67. // If the oauth type is google and the issuer is empty, set the issuer to the default value
  68. if oauthType == OauthTypeGoogle && issuer == "" {
  69. oa.Issuer = IssuerGoogle
  70. }
  71. if oa.PkceEnable == nil {
  72. oa.PkceEnable = new(bool)
  73. *oa.PkceEnable = false
  74. }
  75. if oa.PkceMethod == "" {
  76. oa.PkceMethod = PKCEMethodS256
  77. }
  78. return nil
  79. }
  80. type OauthUser struct {
  81. OpenId string `json:"open_id" gorm:"not null;index"`
  82. Name string `json:"name"`
  83. Username string `json:"username"`
  84. Email string `json:"email"`
  85. VerifiedEmail bool `json:"verified_email,omitempty"`
  86. Picture string `json:"picture,omitempty"`
  87. }
  88. func (ou *OauthUser) ToUser(user *User, overideUsername bool) {
  89. if overideUsername {
  90. user.Username = ou.Username
  91. }
  92. user.Email = ou.Email
  93. user.Nickname = ou.Name
  94. user.Avatar = ou.Picture
  95. }
  96. type OauthUserBase struct {
  97. Name string `json:"name"`
  98. Email string `json:"email"`
  99. }
  100. type OidcUser struct {
  101. OauthUserBase
  102. Sub string `json:"sub"`
  103. VerifiedEmail bool `json:"email_verified"`
  104. PreferredUsername string `json:"preferred_username"`
  105. Picture string `json:"picture"`
  106. }
  107. func (ou *OidcUser) ToOauthUser() *OauthUser {
  108. var username string
  109. // 使用 PreferredUsername,如果不存在,降级到 Email 前缀
  110. if ou.PreferredUsername != "" {
  111. username = ou.PreferredUsername
  112. } else {
  113. username = strings.ToLower(ou.Email)
  114. }
  115. return &OauthUser{
  116. OpenId: ou.Sub,
  117. Name: ou.Name,
  118. Username: username,
  119. Email: ou.Email,
  120. VerifiedEmail: ou.VerifiedEmail,
  121. Picture: ou.Picture,
  122. }
  123. }
  124. type GithubUser struct {
  125. OauthUserBase
  126. Id int `json:"id"`
  127. Login string `json:"login"`
  128. AvatarUrl string `json:"avatar_url"`
  129. VerifiedEmail bool `json:"verified_email"`
  130. }
  131. func (gu *GithubUser) ToOauthUser() *OauthUser {
  132. username := strings.ToLower(gu.Login)
  133. return &OauthUser{
  134. OpenId: strconv.Itoa(gu.Id),
  135. Name: gu.Name,
  136. Username: username,
  137. Email: gu.Email,
  138. VerifiedEmail: gu.VerifiedEmail,
  139. Picture: gu.AvatarUrl,
  140. }
  141. }
  142. type LinuxdoUser struct {
  143. OauthUserBase
  144. Id int `json:"id"`
  145. Username string `json:"username"`
  146. Avatar string `json:"avatar_url"`
  147. }
  148. func (lu *LinuxdoUser) ToOauthUser() *OauthUser {
  149. return &OauthUser{
  150. OpenId: strconv.Itoa(lu.Id),
  151. Name: lu.Name,
  152. Username: strings.ToLower(lu.Username),
  153. Email: lu.Email,
  154. VerifiedEmail: true, // linux.do 用户邮箱默认已验证
  155. Picture: lu.Avatar,
  156. }
  157. }
  158. type OauthList struct {
  159. Oauths []*Oauth `json:"list"`
  160. Pagination
  161. }