summaryrefslogtreecommitdiffstats
path: root/services/auth
diff options
context:
space:
mode:
authorsilverwind <me@silverwind.io>2023-06-13 12:51:02 +0200
committerGitHub <noreply@github.com>2023-06-13 10:51:02 +0000
commita51b115b0ad755562c71f6a382326c59868dfb96 (patch)
tree55a9b74065daad1b51a7ff0835f03e63b11c181a /services/auth
parent2ad2d5a6cea7e0e61bd213167244066c230b054e (diff)
downloadgitea-a51b115b0ad755562c71f6a382326c59868dfb96.tar.gz
gitea-a51b115b0ad755562c71f6a382326c59868dfb96.zip
Use inline SVG for built-in OAuth providers (#25171)
The plan is that all built-in auth providers use inline SVG for more flexibility in styling and to get the GitHub icon to follow `currentcolor`. This only removes the `public/img/auth` directory and adds the missing svgs to our svg build. It should map the built-in providers to these SVGs and render them. If the user has set a Icon URL, it should render that as an `img` tag instead. ``` gitea-azure-ad gitea-bitbucket gitea-discord gitea-dropbox gitea-facebook gitea-gitea gitea-gitlab gitea-google gitea-mastodon gitea-microsoftonline gitea-nextcloud gitea-twitter gitea-yandex octicon-mark-github ``` GitHub logo is now white again on dark theme: <img width="431" alt="Screenshot 2023-06-12 at 21 45 34" src="https://github.com/go-gitea/gitea/assets/115237/27a43504-d60a-4132-a502-336b25883e4d"> --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Diffstat (limited to 'services/auth')
-rw-r--r--services/auth/source/oauth2/providers.go16
-rw-r--r--services/auth/source/oauth2/providers_base.go28
-rw-r--r--services/auth/source/oauth2/providers_openid.go9
3 files changed, 37 insertions, 16 deletions
diff --git a/services/auth/source/oauth2/providers.go b/services/auth/source/oauth2/providers.go
index 0aa83ba494..16620fad6d 100644
--- a/services/auth/source/oauth2/providers.go
+++ b/services/auth/source/oauth2/providers.go
@@ -5,6 +5,9 @@ package oauth2
import (
"errors"
+ "fmt"
+ "html"
+ "html/template"
"net/url"
"sort"
@@ -19,7 +22,7 @@ import (
type Provider interface {
Name() string
DisplayName() string
- IconURL() string
+ IconHTML() template.HTML
CustomURLSettings() *CustomURLSettings
}
@@ -35,7 +38,7 @@ type GothProvider interface {
}
// AuthSourceProvider provides a provider for an AuthSource. Multiple auth sources could use the same registered GothProvider
-// So each auth source should have its own DisplayName and IconURL for display.
+// So each auth source should have its own DisplayName and IconHTML for display.
// The Name is the GothProvider's name, to help to find the GothProvider to sign in.
// The DisplayName is the auth source config's name, site admin set it on the admin page, the IconURL can also be set there.
type AuthSourceProvider struct {
@@ -51,11 +54,14 @@ func (p *AuthSourceProvider) DisplayName() string {
return p.sourceName
}
-func (p *AuthSourceProvider) IconURL() string {
+func (p *AuthSourceProvider) IconHTML() template.HTML {
if p.iconURL != "" {
- return p.iconURL
+ img := fmt.Sprintf(`<img class="gt-mr-3" width="20" height="20" src="%s" alt="%s">`,
+ html.EscapeString(p.iconURL), html.EscapeString(p.DisplayName()),
+ )
+ return template.HTML(img)
}
- return p.GothProvider.IconURL()
+ return p.GothProvider.IconHTML()
}
// Providers contains the map of registered OAuth2 providers in Gitea (based on goth)
diff --git a/services/auth/source/oauth2/providers_base.go b/services/auth/source/oauth2/providers_base.go
index c8e41430ab..5ba06febaf 100644
--- a/services/auth/source/oauth2/providers_base.go
+++ b/services/auth/source/oauth2/providers_base.go
@@ -3,7 +3,12 @@
package oauth2
-import "code.gitea.io/gitea/modules/setting"
+import (
+ "html/template"
+
+ "code.gitea.io/gitea/modules/log"
+ "code.gitea.io/gitea/modules/svg"
+)
// BaseProvider represents a common base for Provider
type BaseProvider struct {
@@ -21,14 +26,21 @@ func (b *BaseProvider) DisplayName() string {
return b.displayName
}
-// IconURL returns an icon path for this provider
-// Use svg for default icons, providers_openid has its own IconURL function
-func (b *BaseProvider) IconURL() string {
- name := b.name
- if b.name == "gplus" {
- name = "google"
+// IconHTML returns icon HTML for this provider
+func (b *BaseProvider) IconHTML() template.HTML {
+ svgName := "gitea-" + b.name
+ switch b.name {
+ case "gplus":
+ svgName = "gitea-google"
+ case "github":
+ svgName = "octicon-mark-github"
+ }
+ svgHTML := svg.RenderHTML(svgName, 20, "gt-mr-3")
+ if svgHTML == "" {
+ log.Error("No SVG icon for oauth2 provider %q", b.name)
+ svgHTML = svg.RenderHTML("gitea-openid", 20, "gt-mr-3")
}
- return setting.AppSubURL + "/assets/img/auth/" + name + ".svg"
+ return svgHTML
}
// CustomURLSettings returns the custom url settings for this provider
diff --git a/services/auth/source/oauth2/providers_openid.go b/services/auth/source/oauth2/providers_openid.go
index c394058d6f..54530ae8a8 100644
--- a/services/auth/source/oauth2/providers_openid.go
+++ b/services/auth/source/oauth2/providers_openid.go
@@ -4,8 +4,11 @@
package oauth2
import (
+ "html/template"
+
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
+ "code.gitea.io/gitea/modules/svg"
"github.com/markbates/goth"
"github.com/markbates/goth/providers/openidConnect"
@@ -24,9 +27,9 @@ func (o *OpenIDProvider) DisplayName() string {
return "OpenID Connect"
}
-// IconURL returns an icon path for this provider
-func (o *OpenIDProvider) IconURL() string {
- return setting.AppSubURL + "/assets/img/svg/gitea-openid.svg"
+// IconHTML returns icon HTML for this provider
+func (o *OpenIDProvider) IconHTML() template.HTML {
+ return svg.RenderHTML("gitea-openid", 20, "gt-mr-3")
}
// CreateGothProvider creates a GothProvider from this Provider