oauth.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package model
  2. import (
  3. "strconv"
  4. "fmt"
  5. )
  6. const (
  7. OauthTypeGithub string = "github"
  8. OauthTypeGoogle string = "google"
  9. OauthTypeOidc string = "oidc"
  10. OauthTypeWebauth string = "webauth"
  11. )
  12. const (
  13. OauthNameGithub string = "GitHub"
  14. OauthNameGoogle string = "Google"
  15. OauthNameOidc string = "OIDC"
  16. OauthNameWebauth string = "WebAuth"
  17. )
  18. const (
  19. UserEndpointGithub string = "https://api.github.com/user"
  20. UserEndpointGoogle string = "https://www.googleapis.com/oauth2/v3/userinfo"
  21. UserEndpointOidc string = ""
  22. )
  23. type Oauth struct {
  24. IdModel
  25. Op string `json:"op"`
  26. OauthType string `json:"oauth_type"`
  27. ClientId string `json:"client_id"`
  28. ClientSecret string `json:"client_secret"`
  29. RedirectUrl string `json:"redirect_url"`
  30. AutoRegister *bool `json:"auto_register"`
  31. Scopes string `json:"scopes"`
  32. Issuer string `json:"issuer"`
  33. TimeModel
  34. }
  35. type OauthUser struct {
  36. OpenId string `json:"open_id" gorm:"not null;index"`
  37. Name string `json:"name"`
  38. Username string `json:"username"`
  39. Email string `json:"email"`
  40. VerifiedEmail bool `json:"verified_email,omitempty"`
  41. Picture string `json:"picture,omitempty"`
  42. }
  43. func (ou *OauthUser) ToUser(user *User, overideUsername bool) {
  44. if overideUsername {
  45. user.Username = ou.Username
  46. }
  47. user.Email = ou.Email
  48. user.Nickname = ou.Name
  49. }
  50. type OauthUserBase struct {
  51. Name string `json:"name"`
  52. Email string `json:"email"`
  53. }
  54. type OidcUser struct {
  55. OauthUserBase
  56. Sub string `json:"sub"`
  57. VerifiedEmail bool `json:"email_verified"`
  58. PreferredUsername string `json:"preferred_username"`
  59. Picture string `json:"picture"`
  60. }
  61. func (ou *OidcUser) ToOauthUser() *OauthUser {
  62. return &OauthUser{
  63. OpenId: ou.Sub,
  64. Name: ou.Name,
  65. Username: ou.PreferredUsername,
  66. Email: ou.Email,
  67. VerifiedEmail: ou.VerifiedEmail,
  68. Picture: ou.Picture,
  69. }
  70. }
  71. type GoogleUser struct {
  72. OauthUserBase
  73. FamilyName string `json:"family_name"`
  74. GivenName string `json:"given_name"`
  75. Id string `json:"id"`
  76. Picture string `json:"picture"`
  77. VerifiedEmail bool `json:"verified_email"`
  78. }
  79. func (gu *GoogleUser) ToOauthUser() *OauthUser {
  80. return &OauthUser{
  81. OpenId: gu.Id,
  82. Name: fmt.Sprintf("%s %s", gu.GivenName, gu.FamilyName),
  83. Username: gu.GivenName,
  84. Email: gu.Email,
  85. VerifiedEmail: gu.VerifiedEmail,
  86. Picture: gu.Picture,
  87. }
  88. }
  89. type GithubUser struct {
  90. OauthUserBase
  91. Id int `json:"id"`
  92. Login string `json:"login"`
  93. AvatarUrl string `json:"avatar_url"`
  94. VerifiedEmail bool `json:"verified_email"`
  95. }
  96. func (gu *GithubUser) ToOauthUser() *OauthUser {
  97. return &OauthUser{
  98. OpenId: strconv.Itoa(gu.Id),
  99. Name: gu.Name,
  100. Username: gu.Login,
  101. Email: gu.Email,
  102. VerifiedEmail: gu.VerifiedEmail,
  103. }
  104. }
  105. type OauthList struct {
  106. Oauths []*Oauth `json:"list"`
  107. Pagination
  108. }