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. user.Avatar = ou.Picture
  50. }
  51. type OauthUserBase struct {
  52. Name string `json:"name"`
  53. Email string `json:"email"`
  54. }
  55. type OidcUser struct {
  56. OauthUserBase
  57. Sub string `json:"sub"`
  58. VerifiedEmail bool `json:"email_verified"`
  59. PreferredUsername string `json:"preferred_username"`
  60. Picture string `json:"picture"`
  61. }
  62. func (ou *OidcUser) ToOauthUser() *OauthUser {
  63. return &OauthUser{
  64. OpenId: ou.Sub,
  65. Name: ou.Name,
  66. Username: ou.PreferredUsername,
  67. Email: ou.Email,
  68. VerifiedEmail: ou.VerifiedEmail,
  69. Picture: ou.Picture,
  70. }
  71. }
  72. type GoogleUser struct {
  73. OauthUserBase
  74. FamilyName string `json:"family_name"`
  75. GivenName string `json:"given_name"`
  76. Id string `json:"id"`
  77. Picture string `json:"picture"`
  78. VerifiedEmail bool `json:"verified_email"`
  79. }
  80. func (gu *GoogleUser) ToOauthUser() *OauthUser {
  81. return &OauthUser{
  82. OpenId: gu.Id,
  83. Name: fmt.Sprintf("%s %s", gu.GivenName, gu.FamilyName),
  84. Username: gu.GivenName,
  85. Email: gu.Email,
  86. VerifiedEmail: gu.VerifiedEmail,
  87. Picture: gu.Picture,
  88. }
  89. }
  90. type GithubUser struct {
  91. OauthUserBase
  92. Id int `json:"id"`
  93. Login string `json:"login"`
  94. AvatarUrl string `json:"avatar_url"`
  95. VerifiedEmail bool `json:"verified_email"`
  96. }
  97. func (gu *GithubUser) ToOauthUser() *OauthUser {
  98. return &OauthUser{
  99. OpenId: strconv.Itoa(gu.Id),
  100. Name: gu.Name,
  101. Username: gu.Login,
  102. Email: gu.Email,
  103. VerifiedEmail: gu.VerifiedEmail,
  104. }
  105. }
  106. type OauthList struct {
  107. Oauths []*Oauth `json:"list"`
  108. Pagination
  109. }