gitea源码

urlmapping.go 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package oauth2
  4. // CustomURLMapping describes the urls values to use when customizing OAuth2 provider URLs
  5. type CustomURLMapping struct {
  6. AuthURL string `json:",omitempty"`
  7. TokenURL string `json:",omitempty"`
  8. ProfileURL string `json:",omitempty"`
  9. EmailURL string `json:",omitempty"`
  10. Tenant string `json:",omitempty"`
  11. }
  12. // CustomURLSettings describes the urls values and availability to use when customizing OAuth2 provider URLs
  13. type CustomURLSettings struct {
  14. AuthURL Attribute
  15. TokenURL Attribute
  16. ProfileURL Attribute
  17. EmailURL Attribute
  18. Tenant Attribute
  19. }
  20. // Attribute describes the availability, and required status for a custom url configuration
  21. type Attribute struct {
  22. Value string
  23. Available bool
  24. Required bool
  25. }
  26. func availableAttribute(value string) Attribute {
  27. return Attribute{Value: value, Available: true}
  28. }
  29. func requiredAttribute(value string) Attribute {
  30. return Attribute{Value: value, Available: true, Required: true}
  31. }
  32. // Required is true if any attribute is required
  33. func (c *CustomURLSettings) Required() bool {
  34. if c == nil {
  35. return false
  36. }
  37. if c.AuthURL.Required || c.EmailURL.Required || c.ProfileURL.Required || c.TokenURL.Required || c.Tenant.Required {
  38. return true
  39. }
  40. return false
  41. }
  42. // OverrideWith copies the current customURLMapping and overrides it with values from the provided mapping
  43. func (c *CustomURLSettings) OverrideWith(override *CustomURLMapping) *CustomURLMapping {
  44. custom := &CustomURLMapping{
  45. AuthURL: c.AuthURL.Value,
  46. TokenURL: c.TokenURL.Value,
  47. ProfileURL: c.ProfileURL.Value,
  48. EmailURL: c.EmailURL.Value,
  49. Tenant: c.Tenant.Value,
  50. }
  51. if override != nil {
  52. if len(override.AuthURL) > 0 && c.AuthURL.Available {
  53. custom.AuthURL = override.AuthURL
  54. }
  55. if len(override.TokenURL) > 0 && c.TokenURL.Available {
  56. custom.TokenURL = override.TokenURL
  57. }
  58. if len(override.ProfileURL) > 0 && c.ProfileURL.Available {
  59. custom.ProfileURL = override.ProfileURL
  60. }
  61. if len(override.EmailURL) > 0 && c.EmailURL.Available {
  62. custom.EmailURL = override.EmailURL
  63. }
  64. if len(override.Tenant) > 0 && c.Tenant.Available {
  65. custom.Tenant = override.Tenant
  66. }
  67. }
  68. return custom
  69. }