oauth.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package admin
  2. import (
  3. "Gwen/model"
  4. "strings"
  5. )
  6. type BindOauthForm struct {
  7. Op string `json:"op" binding:"required"`
  8. }
  9. type OauthConfirmForm struct {
  10. Code string `json:"code" binding:"required"`
  11. }
  12. type UnBindOauthForm struct {
  13. Op string `json:"op" binding:"required"`
  14. }
  15. type OauthForm struct {
  16. Id uint `json:"id"`
  17. Op string `json:"op" validate:"omitempty"`
  18. OauthType string `json:"oauth_type" validate:"required"`
  19. Issuer string `json:"issuer" validate:"omitempty,url"`
  20. Scopes string `json:"scopes" validate:"omitempty"`
  21. ClientId string `json:"client_id" validate:"required"`
  22. ClientSecret string `json:"client_secret" validate:"required"`
  23. RedirectUrl string `json:"redirect_url" validate:"required"`
  24. AutoRegister *bool `json:"auto_register"`
  25. }
  26. func (of *OauthForm) ToOauth() *model.Oauth {
  27. op := strings.ToLower(of.Op)
  28. op = strings.TrimSpace(op)
  29. if op == "" {
  30. switch of.OauthType {
  31. case model.OauthTypeGithub:
  32. of.Op = "GitHub"
  33. case model.OauthTypeGoogle:
  34. of.Op = "Google"
  35. case model.OauthTypeOidc:
  36. of.Op = "OIDC"
  37. case model.OauthTypeWebauth:
  38. of.Op = "WebAuth"
  39. default:
  40. of.Op = of.OauthType
  41. }
  42. }
  43. oa := &model.Oauth{
  44. Op: of.Op,
  45. OauthType: of.OauthType,
  46. ClientId: of.ClientId,
  47. ClientSecret: of.ClientSecret,
  48. RedirectUrl: of.RedirectUrl,
  49. AutoRegister: of.AutoRegister,
  50. Issuer: of.Issuer,
  51. Scopes: of.Scopes,
  52. }
  53. oa.Id = of.Id
  54. return oa
  55. }