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