Skip to content

Commit eeaabf1

Browse files
committed
use IconHTML
1 parent c097d31 commit eeaabf1

File tree

11 files changed

+43
-20
lines changed

11 files changed

+43
-20
lines changed

modules/svg/svg.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,5 @@ func RenderHTML(icon string, others ...interface{}) template.HTML {
6262
}
6363
return template.HTML(svgStr)
6464
}
65-
return template.HTML("")
65+
return ""
6666
}

public/img/svg/gitea-azure-ad.svg

Lines changed: 0 additions & 1 deletion
This file was deleted.

public/img/svg/gitea-azuread.svg

Lines changed: 1 addition & 0 deletions
Loading

public/img/svg/gitea-azureadv2.svg

Lines changed: 1 addition & 0 deletions
Loading

services/auth/source/oauth2/providers.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ package oauth2
55

66
import (
77
"errors"
8+
"fmt"
9+
"html"
10+
"html/template"
811
"net/url"
912
"sort"
1013

@@ -19,7 +22,7 @@ import (
1922
type Provider interface {
2023
Name() string
2124
DisplayName() string
22-
IconURL() string
25+
IconHTML() template.HTML
2326
CustomURLSettings() *CustomURLSettings
2427
}
2528

@@ -35,7 +38,7 @@ type GothProvider interface {
3538
}
3639

3740
// AuthSourceProvider provides a provider for an AuthSource. Multiple auth sources could use the same registered GothProvider
38-
// So each auth source should have its own DisplayName and IconURL for display.
41+
// So each auth source should have its own DisplayName and IconHTML for display.
3942
// The Name is the GothProvider's name, to help to find the GothProvider to sign in.
4043
// The DisplayName is the auth source config's name, site admin set it on the admin page, the IconURL can also be set there.
4144
type AuthSourceProvider struct {
@@ -51,11 +54,14 @@ func (p *AuthSourceProvider) DisplayName() string {
5154
return p.sourceName
5255
}
5356

54-
func (p *AuthSourceProvider) IconURL() string {
57+
func (p *AuthSourceProvider) IconHTML() template.HTML {
5558
if p.iconURL != "" {
56-
return p.iconURL
59+
img := fmt.Sprintf(`<img class="gt-mr-3" width="20" height="20" src="%s" alt="{{%s}}">`,
60+
html.EscapeString(p.iconURL), html.EscapeString(p.DisplayName()),
61+
)
62+
return template.HTML(img)
5763
}
58-
return p.GothProvider.IconURL()
64+
return p.GothProvider.IconHTML()
5965
}
6066

6167
// Providers contains the map of registered OAuth2 providers in Gitea (based on goth)

services/auth/source/oauth2/providers_base.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33

44
package oauth2
55

6-
import "code.gitea.io/gitea/modules/setting"
6+
import (
7+
"html/template"
8+
9+
"code.gitea.io/gitea/modules/log"
10+
"code.gitea.io/gitea/modules/svg"
11+
)
712

813
// BaseProvider represents a common base for Provider
914
type BaseProvider struct {
@@ -21,14 +26,21 @@ func (b *BaseProvider) DisplayName() string {
2126
return b.displayName
2227
}
2328

24-
// IconURL returns an icon path for this provider
25-
// Use svg for default icons, providers_openid has its own IconURL function
26-
func (b *BaseProvider) IconURL() string {
27-
name := b.name
28-
if b.name == "gplus" {
29-
name = "google"
29+
// IconHTML returns icon HTML for this provider
30+
func (b *BaseProvider) IconHTML() template.HTML {
31+
svgName := "gitea-" + b.name
32+
switch b.name {
33+
case "gplus":
34+
svgName = "gitea-google"
35+
case "github":
36+
svgName = "octicon-mark-github"
37+
}
38+
svgHTML := svg.RenderHTML(svgName, 20, "gt-mr-3")
39+
if svgHTML == "" {
40+
log.Error("No SVG icon for oauth2 provider %q", b.name)
41+
svgHTML = svg.RenderHTML("gitea-openid", 20, "gt-mr-3")
3042
}
31-
return setting.AppSubURL + "/assets/img/auth/" + name + ".svg"
43+
return svgHTML
3244
}
3345

3446
// CustomURLSettings returns the custom url settings for this provider

services/auth/source/oauth2/providers_openid.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
package oauth2
55

66
import (
7+
"html/template"
8+
79
"code.gitea.io/gitea/modules/log"
810
"code.gitea.io/gitea/modules/setting"
11+
"code.gitea.io/gitea/modules/svg"
912

1013
"github.com/markbates/goth"
1114
"github.com/markbates/goth/providers/openidConnect"
@@ -24,9 +27,9 @@ func (o *OpenIDProvider) DisplayName() string {
2427
return "OpenID Connect"
2528
}
2629

27-
// IconURL returns an icon path for this provider
28-
func (o *OpenIDProvider) IconURL() string {
29-
return setting.AppSubURL + "/assets/img/svg/gitea-openid.svg"
30+
// IconHTML returns icon HTML for this provider
31+
func (o *OpenIDProvider) IconHTML() template.HTML {
32+
return svg.RenderHTML("gitea-openid", 20, "gt-mr-3")
3033
}
3134

3235
// CreateGothProvider creates a GothProvider from this Provider

templates/user/auth/signin_inner.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
{{range $key := .OrderedOAuth2Names}}
6363
{{$provider := index $.OAuth2Providers $key}}
6464
<a class="{{$provider.Name}} ui button gt-df gt-ac gt-jc gt-py-3 oauth-login-link" href="{{AppSubUrl}}/user/oauth2/{{$key}}">
65-
<img class="gt-mr-3" width="20" height="20" src="{{$provider.IconURL}}" alt="{{$provider.DisplayName}}">
65+
{{$provider.IconHTML}}
6666
{{$.locale.Tr "sign_in_with_provider" $provider.DisplayName}}
6767
</a>
6868
{{end}}

templates/user/settings/security/accountlinks.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
{{range $key := .OrderedOAuth2Names}}
99
{{$provider := index $.OAuth2Providers $key}}
1010
<a class="item" href="{{AppSubUrl}}/user/oauth2/{{$key}}">
11-
<img class="gt-mr-3" width="20" height="20" src="{{$provider.IconURL}}" alt="{{$provider.DisplayName}}">
11+
{{$provider.IconHTML}}
1212
{{$provider.DisplayName}}
1313
</a>
1414
{{end}}
File renamed without changes.

web_src/svg/gitea-azureadv2.svg

Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)