You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

source.go 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package oauth2
  4. import (
  5. "code.gitea.io/gitea/models/auth"
  6. "code.gitea.io/gitea/modules/json"
  7. "code.gitea.io/gitea/modules/secret"
  8. "code.gitea.io/gitea/modules/setting"
  9. )
  10. // Source holds configuration for the OAuth2 login source.
  11. type Source struct {
  12. Provider string
  13. ClientID string
  14. ClientSecret string
  15. ClientSecretEncrypt string // Encrypted Client Secret
  16. OpenIDConnectAutoDiscoveryURL string
  17. CustomURLMapping *CustomURLMapping
  18. IconURL string
  19. Scopes []string
  20. RequiredClaimName string
  21. RequiredClaimValue string
  22. GroupClaimName string
  23. AdminGroup string
  24. GroupTeamMap string
  25. GroupTeamMapRemoval bool
  26. RestrictedGroup string
  27. SkipLocalTwoFA bool `json:",omitempty"`
  28. // reference to the authSource
  29. authSource *auth.Source
  30. }
  31. // FromDB fills up an OAuth2Config from serialized format.
  32. func (source *Source) FromDB(bs []byte) error {
  33. err := json.UnmarshalHandleDoubleEncode(bs, &source)
  34. if err != nil {
  35. return err
  36. }
  37. if source.ClientSecretEncrypt != "" {
  38. source.ClientSecret, err = secret.DecryptSecret(setting.SecretKey, source.ClientSecretEncrypt)
  39. source.ClientSecretEncrypt = ""
  40. }
  41. return err
  42. }
  43. // ToDB exports an OAuth2Config to a serialized format.
  44. func (source *Source) ToDB() ([]byte, error) {
  45. var err error
  46. source.ClientSecretEncrypt, err = secret.EncryptSecret(setting.SecretKey, source.ClientSecret)
  47. if err != nil {
  48. return nil, err
  49. }
  50. source.ClientSecret = ""
  51. return json.Marshal(source)
  52. }
  53. // SetAuthSource sets the related AuthSource
  54. func (source *Source) SetAuthSource(authSource *auth.Source) {
  55. source.authSource = authSource
  56. }
  57. func init() {
  58. auth.RegisterTypeConfig(auth.OAuth2, &Source{})
  59. }