Skip to content

File tree

6 files changed

+48
-10
lines changed

6 files changed

+48
-10
lines changed

Diff for: options/locale/locale_en-US.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -2901,7 +2901,7 @@ auths.sspi_default_language = Default user language
29012901
auths.sspi_default_language_helper = Default language for users automatically created by SSPI auth method. Leave empty if you prefer language to be automatically detected.
29022902
auths.tips = Tips
29032903
auths.tips.oauth2.general = OAuth2 Authentication
2904-
auths.tips.oauth2.general.tip = When registering a new OAuth2 authentication, the callback/redirect URL should be: <host>/user/oauth2/<Authentication Name>/callback
2904+
auths.tips.oauth2.general.tip = When registering a new OAuth2 authentication, the callback/redirect URL should be:
29052905
auths.tip.oauth2_provider = OAuth2 Provider
29062906
auths.tip.bitbucket = Register a new OAuth consumer on https://bitbucket.org/account/user/<your username>/oauth-consumers/new and add the permission 'Account' - 'Read'
29072907
auths.tip.nextcloud = Register a new OAuth consumer on your instance using the following menu "Settings -> Security -> OAuth 2.0 client"

Diff for: routers/web/auth/oauth.go

+19-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
package auth
55

66
import (
7-
stdContext "context"
7+
go_context "context"
88
"encoding/base64"
99
"errors"
1010
"fmt"
1111
"html"
1212
"io"
1313
"net/http"
1414
"net/url"
15+
"sort"
1516
"strings"
1617

1718
"code.gitea.io/gitea/models/auth"
@@ -39,6 +40,7 @@ import (
3940
"github.com/golang-jwt/jwt/v4"
4041
"github.com/markbates/goth"
4142
"github.com/markbates/goth/gothic"
43+
go_oauth2 "golang.org/x/oauth2"
4244
)
4345

4446
const (
@@ -143,7 +145,7 @@ type AccessTokenResponse struct {
143145
IDToken string `json:"id_token,omitempty"`
144146
}
145147

146-
func newAccessTokenResponse(ctx stdContext.Context, grant *auth.OAuth2Grant, serverKey, clientKey oauth2.JWTSigningKey) (*AccessTokenResponse, *AccessTokenError) {
148+
func newAccessTokenResponse(ctx go_context.Context, grant *auth.OAuth2Grant, serverKey, clientKey oauth2.JWTSigningKey) (*AccessTokenResponse, *AccessTokenError) {
147149
if setting.OAuth2.InvalidateRefreshTokens {
148150
if err := grant.IncreaseCounter(ctx); err != nil {
149151
return nil, &AccessTokenError{
@@ -886,6 +888,17 @@ func SignInOAuth(ctx *context.Context) {
886888
func SignInOAuthCallback(ctx *context.Context) {
887889
provider := ctx.Params(":provider")
888890

891+
if ctx.Req.FormValue("error") != "" {
892+
var errorKeyValues []string
893+
for k, vv := range ctx.Req.Form {
894+
for _, v := range vv {
895+
errorKeyValues = append(errorKeyValues, fmt.Sprintf("%s = %s", html.EscapeString(k), html.EscapeString(v)))
896+
}
897+
}
898+
sort.Strings(errorKeyValues)
899+
ctx.Flash.Error(strings.Join(errorKeyValues, "<br>"), true)
900+
}
901+
889902
// first look if the provider is still active
890903
authSource, err := auth.GetActiveOAuth2SourceByName(provider)
891904
if err != nil {
@@ -894,7 +907,7 @@ func SignInOAuthCallback(ctx *context.Context) {
894907
}
895908

896909
if authSource == nil {
897-
ctx.ServerError("SignIn", errors.New("No valid provider found, check configured callback url in provider"))
910+
ctx.ServerError("SignIn", errors.New("no valid provider found, check configured callback url in provider"))
898911
return
899912
}
900913

@@ -920,6 +933,9 @@ func SignInOAuthCallback(ctx *context.Context) {
920933
ctx.Redirect(setting.AppSubURL + "/user/login")
921934
return
922935
}
936+
if err, ok := err.(*go_oauth2.RetrieveError); ok {
937+
ctx.Flash.Error("OAuth2 RetrieveError: "+err.Error(), true)
938+
}
923939
ctx.ServerError("UserSignIn", err)
924940
return
925941
}

Diff for: templates/admin/auth/edit.tmpl

+13-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
<span>{{.Source.TypeName}}</span>
1515
</div>
1616
<div class="required inline field {{if .Err_Name}}error{{end}}">
17-
<label for="name">{{.locale.Tr "admin.auths.auth_name"}}</label>
18-
<input id="name" name="name" value="{{.Source.Name}}" autofocus required>
17+
<label for="auth_name">{{.locale.Tr "admin.auths.auth_name"}}</label>
18+
<input id="auth_name" name="name" value="{{.Source.Name}}" autofocus required>
1919
</div>
2020

2121
<!-- LDAP and DLDAP -->
@@ -434,6 +434,17 @@
434434
</div>
435435
</form>
436436
</div>
437+
438+
<h4 class="ui top attached header">
439+
{{.locale.Tr "admin.auths.tips"}}
440+
</h4>
441+
<div class="ui attached segment">
442+
<h5>GMail Settings:</h5>
443+
<p>Host: smtp.gmail.com, Port: 587, Enable TLS Encryption: true</p>
444+
445+
<h5 class="oauth2">{{.locale.Tr "admin.auths.tips.oauth2.general"}}:</h5>
446+
<p class="oauth2">{{.locale.Tr "admin.auths.tips.oauth2.general.tip"}} <b id="oauth2-callback-url"></b></p>
447+
</div>
437448
</div>
438449

439450
<div class="ui g-modal-confirm delete modal">

Diff for: templates/admin/auth/new.tmpl

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
</div>
2323
</div>
2424
<div class="required inline field {{if .Err_Name}}error{{end}}">
25-
<label for="name">{{.locale.Tr "admin.auths.auth_name"}}</label>
26-
<input id="name" name="name" value="{{.name}}" autofocus required>
25+
<label for="auth_name">{{.locale.Tr "admin.auths.auth_name"}}</label>
26+
<input id="auth_name" name="name" value="{{.name}}" autofocus required>
2727
</div>
2828

2929
<!-- LDAP and DLDAP -->
@@ -85,8 +85,8 @@
8585
<h5>GMail Settings:</h5>
8686
<p>Host: smtp.gmail.com, Port: 587, Enable TLS Encryption: true</p>
8787

88-
<h5>{{.locale.Tr "admin.auths.tips.oauth2.general"}}:</h5>
89-
<p>{{.locale.Tr "admin.auths.tips.oauth2.general.tip"}}</p>
88+
<h5 class="oauth2">{{.locale.Tr "admin.auths.tips.oauth2.general"}}:</h5>
89+
<p class="oauth2">{{.locale.Tr "admin.auths.tips.oauth2.general.tip"}} <b id="oauth2-callback-url"></b></p>
9090

9191
<h5 class="ui top attached header">{{.locale.Tr "admin.auths.tip.oauth2_provider"}}</h5>
9292
<div class="ui attached segment">

Diff for: templates/status/500.tmpl

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{{/* This page should only depend the minimal template functions/variables, to avoid triggering new panics.
22
* base template functions: AppName, AssetUrlPrefix, AssetVersion, AppSubUrl, DefaultTheme, Str2html
33
* locale
4+
* Flash
45
* ErrorMsg
56
* SignedUser (optional)
67
*/}}
@@ -28,6 +29,10 @@
2829
</div>
2930
</nav>
3031
<div role="main" class="page-content status-page-500">
32+
<div class="ui container" >
33+
<style> .ui.message.flash-message { text-align: left; } </style>
34+
{{template "base/alert" .}}
35+
</div>
3136
<p class="gt-mt-5 center"><img src="{{AssetUrlPrefix}}/img/500.png" alt="Internal Server Error"></p>
3237
<div class="ui divider"></div>
3338
<div class="ui container gt-my-5">

Diff for: web_src/js/features/admin/common.js

+6
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,12 @@ export function initAdminCommon() {
171171
}
172172
}
173173

174+
if ($('.admin.authentication').length > 0) {
175+
$('#auth_name').on('input', function () {
176+
$('#oauth2-callback-url').text(`${window.location.origin}/user/oauth2/${encodeURIComponent($(this).val())}/callback`);
177+
}).trigger('input');
178+
}
179+
174180
// Notice
175181
if ($('.admin.notice')) {
176182
const $detailModal = $('#detail-modal');

0 commit comments

Comments
 (0)