From cd5278a44c45ad4f763b7142674c956576561d43 Mon Sep 17 00:00:00 2001 From: silverwind Date: Wed, 23 Dec 2020 20:09:54 +0100 Subject: [PATCH 01/46] Fix manifest encoding (#14114) The previous URL encoding would encode spaces to '+' for the app name which is incorrect. Use base64 encoding instead which does not have such issues. --- modules/setting/setting.go | 84 +++++++++++++++++++++++++++++---- modules/setting/setting_test.go | 29 ++++++++++++ templates/base/head.tmpl | 2 +- 3 files changed, 105 insertions(+), 10 deletions(-) create mode 100644 modules/setting/setting_test.go diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 290ec94c4424..8a4d7acc4d13 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -7,8 +7,8 @@ package setting import ( "encoding/base64" + "encoding/json" "fmt" - "html/template" "io" "io/ioutil" "math" @@ -104,6 +104,7 @@ var ( GracefulHammerTime time.Duration StartupTimeout time.Duration StaticURLPrefix string + AbsoluteAssetURL string SSH = struct { Disabled bool `ini:"DISABLE_SSH"` @@ -294,7 +295,7 @@ var ( CSRFCookieName = "_csrf" CSRFCookieHTTPOnly = true - ManifestData template.URL + ManifestData string // Mirror settings Mirror struct { @@ -600,6 +601,11 @@ func NewContext() { Domain = urlHostname } + AbsoluteAssetURL = MakeAbsoluteAssetURL(AppURL, StaticURLPrefix) + + manifestBytes := MakeManifestData(AppName, AppURL, AbsoluteAssetURL) + ManifestData = `application/json;base64,` + base64.StdEncoding.EncodeToString(manifestBytes) + var defaultLocalURL string switch Protocol { case UnixSocket: @@ -645,8 +651,6 @@ func NewContext() { LandingPageURL = LandingPageHome } - ManifestData = makeManifestData() - if len(SSH.Domain) == 0 { SSH.Domain = Domain } @@ -1045,12 +1049,74 @@ func loadOrGenerateInternalToken(sec *ini.Section) string { return token } -func makeManifestData() template.URL { - name := url.QueryEscape(AppName) - prefix := url.QueryEscape(StaticURLPrefix) - subURL := url.QueryEscape(AppSubURL) + "/" +// MakeAbsoluteAssetURL returns the absolute asset url prefix without a trailing slash +func MakeAbsoluteAssetURL(appURL string, staticURLPrefix string) string { + parsedPrefix, err := url.Parse(strings.TrimSuffix(staticURLPrefix, "/")) + if err != nil { + log.Fatal("Unable to parse STATIC_URL_PREFIX: %v", err) + } + + if err == nil && parsedPrefix.Hostname() == "" { + if staticURLPrefix == "" { + return strings.TrimSuffix(appURL, "/") + } + + // StaticURLPrefix is just a path + return strings.TrimSuffix(appURL, "/") + strings.TrimSuffix(staticURLPrefix, "/") + } + + return strings.TrimSuffix(staticURLPrefix, "/") +} + +// MakeManifestData generates web app manifest JSON +func MakeManifestData(appName string, appURL string, absoluteAssetURL string) []byte { + type manifestIcon struct { + Src string `json:"src"` + Type string `json:"type"` + Sizes string `json:"sizes"` + } + + type manifestJSON struct { + Name string `json:"name"` + ShortName string `json:"short_name"` + StartURL string `json:"start_url"` + Icons []manifestIcon `json:"icons"` + } + + bytes, err := json.Marshal(&manifestJSON{ + Name: appName, + ShortName: appName, + StartURL: appURL, + Icons: []manifestIcon{ + { + Src: absoluteAssetURL + "/img/logo-lg.png", + Type: "image/png", + Sizes: "880x880", + }, + { + Src: absoluteAssetURL + "/img/logo-512.png", + Type: "image/png", + Sizes: "512x512", + }, + { + Src: absoluteAssetURL + "/img/logo-192.png", + Type: "image/png", + Sizes: "192x192", + }, + { + Src: absoluteAssetURL + "/img/logo-sm.png", + Type: "image/png", + Sizes: "120x120", + }, + }, + }) + + if err != nil { + log.Error("unable to marshal manifest JSON. Error: %v", err) + return make([]byte, 0) + } - return template.URL(`data:application/json,{"short_name":"` + name + `","name":"` + name + `","icons":[{"src":"` + prefix + `/img/logo-lg.png","type":"image/png","sizes":"880x880"},{"src":"` + prefix + `/img/logo-sm.png","type":"image/png","sizes":"120x120"},{"src":"` + prefix + `/img/logo-512.png","type":"image/png","sizes":"512x512"},{"src":"` + prefix + `/img/logo-192.png","type":"image/png","sizes":"192x192"}],"start_url":"` + subURL + `","scope":"` + subURL + `","background_color":"%23FAFAFA","display":"standalone"}`) + return bytes } // NewServices initializes the services diff --git a/modules/setting/setting_test.go b/modules/setting/setting_test.go new file mode 100644 index 000000000000..f12fd8843a95 --- /dev/null +++ b/modules/setting/setting_test.go @@ -0,0 +1,29 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package setting + +import ( + "encoding/json" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestMakeAbsoluteAssetURL(t *testing.T) { + assert.Equal(t, "https://localhost:2345", MakeAbsoluteAssetURL("https://localhost:1234", "https://localhost:2345")) + assert.Equal(t, "https://localhost:2345", MakeAbsoluteAssetURL("https://localhost:1234/", "https://localhost:2345")) + assert.Equal(t, "https://localhost:2345", MakeAbsoluteAssetURL("https://localhost:1234/", "https://localhost:2345/")) + assert.Equal(t, "https://localhost:1234/foo", MakeAbsoluteAssetURL("https://localhost:1234", "/foo")) + assert.Equal(t, "https://localhost:1234/foo", MakeAbsoluteAssetURL("https://localhost:1234/", "/foo")) + assert.Equal(t, "https://localhost:1234/foo", MakeAbsoluteAssetURL("https://localhost:1234/", "/foo/")) + assert.Equal(t, "https://localhost:1234/foo/bar", MakeAbsoluteAssetURL("https://localhost:1234/foo", "/bar")) + assert.Equal(t, "https://localhost:1234/foo/bar", MakeAbsoluteAssetURL("https://localhost:1234/foo/", "/bar")) + assert.Equal(t, "https://localhost:1234/foo/bar", MakeAbsoluteAssetURL("https://localhost:1234/foo/", "/bar/")) +} + +func TestMakeManifestData(t *testing.T) { + jsonBytes := MakeManifestData(`Example App '\"`, "https://example.com", "https://example.com/foo/bar") + assert.True(t, json.Valid(jsonBytes)) +} diff --git a/templates/base/head.tmpl b/templates/base/head.tmpl index c47fd08c1772..32660df6bbf8 100644 --- a/templates/base/head.tmpl +++ b/templates/base/head.tmpl @@ -5,7 +5,7 @@ {{if .Title}}{{.Title | RenderEmojiPlain}} - {{end}} {{if .Repository.Name}}{{.Repository.Name}} - {{end}}{{AppName}} - + From a9876bca82e2c32ad39f8020c3f55b68f78f523e Mon Sep 17 00:00:00 2001 From: GiteaBot Date: Wed, 23 Dec 2020 19:11:05 +0000 Subject: [PATCH 02/46] [skip ci] Updated translations via Crowdin --- options/locale/locale_zh-TW.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/options/locale/locale_zh-TW.ini b/options/locale/locale_zh-TW.ini index b85a982dbb07..6bc9688cdba0 100644 --- a/options/locale/locale_zh-TW.ini +++ b/options/locale/locale_zh-TW.ini @@ -1835,7 +1835,7 @@ release.prerelease_helper=標記此版本不適合生產使用。 release.cancel=取消 release.publish=發佈版本 release.save_draft=儲存草稿 -release.edit_release=編輯發佈訊息 +release.edit_release=更新發佈 release.delete_release=刪除發佈 release.delete_tag=刪除標籤 release.deletion=刪除發佈 From 5b958315c19ab686f351068681631e19eebd1d9a Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 23 Dec 2020 19:52:46 +0000 Subject: [PATCH 03/46] API: Make BasicAuth Prefix case insensitive (#14106) --- modules/auth/sso/basic.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/auth/sso/basic.go b/modules/auth/sso/basic.go index b5885d38db1e..aab4eceebccd 100644 --- a/modules/auth/sso/basic.go +++ b/modules/auth/sso/basic.go @@ -56,7 +56,7 @@ func (b *Basic) VerifyAuthData(ctx *macaron.Context, sess session.Store) *models } auths := strings.Fields(baHead) - if len(auths) != 2 || auths[0] != "Basic" { + if len(auths) != 2 || (auths[0] != "Basic" && auths[0] != "basic") { return nil } From 87a0396719dc12a19e9876bd4e5ba6ea008072d8 Mon Sep 17 00:00:00 2001 From: silverwind Date: Wed, 23 Dec 2020 21:38:29 +0100 Subject: [PATCH 04/46] Fix admin monitoring margin (#14134) --- templates/admin/monitor.tmpl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/admin/monitor.tmpl b/templates/admin/monitor.tmpl index 272ebff6c100..a8431cbd779c 100644 --- a/templates/admin/monitor.tmpl +++ b/templates/admin/monitor.tmpl @@ -8,8 +8,6 @@
- - {{.CsrfTokenHtml}} @@ -34,6 +32,8 @@ {{end}}
+ + {{.CsrfTokenHtml}}
From 19ae6439b0956a578100b50cb09b1cbd40a01942 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 24 Dec 2020 12:25:17 +0800 Subject: [PATCH 05/46] Improve vfsgen to not unzip bindata files but send to browser directly (#7109) * Don't unzip files from bindata but send to browser directly * remove dependent for httpgzip * Add tests for parseAcceptEncoding * Update docs for ENABLE_GZIP * Fix bug * Fix bug Co-authored-by: zeripath --- custom/conf/app.example.ini | 2 +- .../doc/advanced/config-cheat-sheet.en-us.md | 2 +- .../doc/advanced/config-cheat-sheet.zh-cn.md | 2 +- modules/public/dynamic.go | 12 +++++- modules/public/public.go | 12 +++++- modules/public/public_test.go | 40 +++++++++++++++++++ modules/public/static.go | 40 +++++++++++++++++++ 7 files changed, 105 insertions(+), 5 deletions(-) create mode 100644 modules/public/public_test.go diff --git a/custom/conf/app.example.ini b/custom/conf/app.example.ini index b89bbf894e50..e26e9e4d5690 100644 --- a/custom/conf/app.example.ini +++ b/custom/conf/app.example.ini @@ -361,7 +361,7 @@ KEY_FILE = https/key.pem STATIC_ROOT_PATH = ; Default path for App data APP_DATA_PATH = data -; Application level GZIP support +; Enable gzip compression for runtime-generated content, static resources excluded ENABLE_GZIP = false ; Application profiling (memory and cpu) ; For "web" command it listens on localhost:6060 diff --git a/docs/content/doc/advanced/config-cheat-sheet.en-us.md b/docs/content/doc/advanced/config-cheat-sheet.en-us.md index d482523f797b..43f42b95e02f 100644 --- a/docs/content/doc/advanced/config-cheat-sheet.en-us.md +++ b/docs/content/doc/advanced/config-cheat-sheet.en-us.md @@ -264,7 +264,7 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`. - `STATIC_ROOT_PATH`: **./**: Upper level of template and static files path. - `APP_DATA_PATH`: **data** (**/data/gitea** on docker): Default path for application data. - `STATIC_CACHE_TIME`: **6h**: Web browser cache time for static resources on `custom/`, `public/` and all uploaded avatars. Note that this cache is disabled when `RUN_MODE` is "dev". -- `ENABLE_GZIP`: **false**: Enables application-level GZIP support. +- `ENABLE_GZIP`: **false**: Enable gzip compression for runtime-generated content, static resources excluded. - `ENABLE_PPROF`: **false**: Application profiling (memory and cpu). For "web" command it listens on localhost:6060. For "serv" command it dumps to disk at `PPROF_DATA_PATH` as `(cpuprofile|memprofile)__` - `PPROF_DATA_PATH`: **data/tmp/pprof**: `PPROF_DATA_PATH`, use an absolute path when you start gitea as service - `LANDING_PAGE`: **home**: Landing page for unauthenticated users \[home, explore, organizations, login\]. diff --git a/docs/content/doc/advanced/config-cheat-sheet.zh-cn.md b/docs/content/doc/advanced/config-cheat-sheet.zh-cn.md index c1f7e836c01a..da2d02c11dcc 100644 --- a/docs/content/doc/advanced/config-cheat-sheet.zh-cn.md +++ b/docs/content/doc/advanced/config-cheat-sheet.zh-cn.md @@ -70,7 +70,7 @@ menu: - `KEY_FILE`: 启用HTTPS的密钥文件。 - `STATIC_ROOT_PATH`: 存放模板和静态文件的根目录,默认是 Gitea 的根目录。 - `STATIC_CACHE_TIME`: **6h**: 静态资源文件,包括 `custom/`, `public/` 和所有上传的头像的浏览器缓存时间。 -- `ENABLE_GZIP`: 启用应用级别的 GZIP 压缩。 +- `ENABLE_GZIP`: 启用实时生成的数据启用 GZIP 压缩,不包括静态资源。 - `LANDING_PAGE`: 未登录用户的默认页面,可选 `home` 或 `explore`。 - `LFS_START_SERVER`: 是否启用 git-lfs 支持. 可以为 `true` 或 `false`, 默认是 `false`。 diff --git a/modules/public/dynamic.go b/modules/public/dynamic.go index f1a4dbb1a365..f634c598a3f1 100644 --- a/modules/public/dynamic.go +++ b/modules/public/dynamic.go @@ -6,9 +6,19 @@ package public -import "net/http" +import ( + "io" + "net/http" + "os" + "time" +) // Static implements the macaron static handler for serving assets. func Static(opts *Options) func(next http.Handler) http.Handler { return opts.staticHandler(opts.Directory) } + +// ServeContent serve http content +func ServeContent(w http.ResponseWriter, req *http.Request, fi os.FileInfo, modtime time.Time, content io.ReadSeeker) { + http.ServeContent(w, req, fi.Name(), modtime, content) +} diff --git a/modules/public/public.go b/modules/public/public.go index fc933637d8f6..c8148e6db36f 100644 --- a/modules/public/public.go +++ b/modules/public/public.go @@ -87,6 +87,16 @@ func (opts *Options) staticHandler(dir string) func(next http.Handler) http.Hand } } +// parseAcceptEncoding parse Accept-Encoding: deflate, gzip;q=1.0, *;q=0.5 as compress methods +func parseAcceptEncoding(val string) map[string]bool { + parts := strings.Split(val, ";") + var types = make(map[string]bool) + for _, v := range strings.Split(parts[0], ",") { + types[strings.TrimSpace(v)] = true + } + return types +} + func (opts *Options) handle(w http.ResponseWriter, req *http.Request, opt *Options) bool { if req.Method != "GET" && req.Method != "HEAD" { return false @@ -157,6 +167,6 @@ func (opts *Options) handle(w http.ResponseWriter, req *http.Request, opt *Optio return true } - http.ServeContent(w, req, file, fi.ModTime(), f) + ServeContent(w, req, fi, fi.ModTime(), f) return true } diff --git a/modules/public/public_test.go b/modules/public/public_test.go new file mode 100644 index 000000000000..cf8dced43114 --- /dev/null +++ b/modules/public/public_test.go @@ -0,0 +1,40 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package public + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestParseAcceptEncoding(t *testing.T) { + var kases = []struct { + Header string + Expected map[string]bool + }{ + { + Header: "deflate, gzip;q=1.0, *;q=0.5", + Expected: map[string]bool{ + "deflate": true, + "gzip": true, + }, + }, + { + Header: " gzip, deflate, br", + Expected: map[string]bool{ + "deflate": true, + "gzip": true, + "br": true, + }, + }, + } + + for _, kase := range kases { + t.Run(kase.Header, func(t *testing.T) { + assert.EqualValues(t, kase.Expected, parseAcceptEncoding(kase.Header)) + }) + } +} diff --git a/modules/public/static.go b/modules/public/static.go index 8da10567ead7..c4dd7a1eca75 100644 --- a/modules/public/static.go +++ b/modules/public/static.go @@ -7,8 +7,17 @@ package public import ( + "bytes" + "compress/gzip" + "io" "io/ioutil" + "mime" "net/http" + "os" + "path/filepath" + "time" + + "code.gitea.io/gitea/modules/log" ) // Static implements the macaron static handler for serving assets. @@ -49,3 +58,34 @@ func AssetIsDir(name string) (bool, error) { } } } + +// ServeContent serve http content +func ServeContent(w http.ResponseWriter, req *http.Request, fi os.FileInfo, modtime time.Time, content io.ReadSeeker) { + encodings := parseAcceptEncoding(req.Header.Get("Accept-Encoding")) + if encodings["gzip"] { + if cf, ok := fi.(*vfsgen۰CompressedFileInfo); ok { + rd := bytes.NewReader(cf.GzipBytes()) + w.Header().Set("Content-Encoding", "gzip") + ctype := mime.TypeByExtension(filepath.Ext(fi.Name())) + if ctype == "" { + // read a chunk to decide between utf-8 text and binary + var buf [512]byte + grd, _ := gzip.NewReader(rd) + n, _ := io.ReadFull(grd, buf[:]) + ctype = http.DetectContentType(buf[:n]) + _, err := rd.Seek(0, io.SeekStart) // rewind to output whole file + if err != nil { + log.Error("rd.Seek error: %v", err) + http.Error(w, http.StatusText(500), 500) + return + } + } + w.Header().Set("Content-Type", ctype) + http.ServeContent(w, req, fi.Name(), modtime, rd) + return + } + } + + http.ServeContent(w, req, fi.Name(), modtime, content) + return +} From 4c2a1c01a84d6718441863682d44d4a5635af703 Mon Sep 17 00:00:00 2001 From: GiteaBot Date: Thu, 24 Dec 2020 04:26:32 +0000 Subject: [PATCH 06/46] [skip ci] Updated translations via Crowdin --- options/locale/locale_de-DE.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/options/locale/locale_de-DE.ini b/options/locale/locale_de-DE.ini index f5904228ab80..3734779c6803 100644 --- a/options/locale/locale_de-DE.ini +++ b/options/locale/locale_de-DE.ini @@ -429,7 +429,7 @@ uid=Uid u2f=Hardware-Sicherheitsschlüssel public_profile=Öffentliches Profil -biography_placeholder=Erzähle uns noch ein bisschen was über dich +biography_placeholder=Erzähle uns noch ein bisschen über dich profile_desc=Deine E-Mail-Adresse wird für Benachrichtigungen und anderes verwendet. password_username_disabled=Benutzer, die nicht von Gitea verwaltet werden können ihren Benutzernamen nicht ändern. Bitte kontaktiere deinen Administrator für mehr Details. full_name=Vollständiger Name From fa2e34928d56598268bbf37e175a291a4b40e92c Mon Sep 17 00:00:00 2001 From: zeripath Date: Thu, 24 Dec 2020 15:26:19 +0000 Subject: [PATCH 07/46] Ensure that search term and page are not lost on adoption page-turn (#14133) Fix #14111 Signed-off-by: Andrew Thornton Co-authored-by: Lunny Xiao --- routers/admin/repos.go | 11 ++++++++++- templates/admin/repo/unadopted.tmpl | 4 ++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/routers/admin/repos.go b/routers/admin/repos.go index 54b6c8e7c6fb..46d0b60f2498 100644 --- a/routers/admin/repos.go +++ b/routers/admin/repos.go @@ -5,6 +5,8 @@ package admin import ( + "net/url" + "strconv" "strings" "code.gitea.io/gitea/models" @@ -71,6 +73,8 @@ func UnadoptedRepos(ctx *context.Context) { opts.Page = 1 } + ctx.Data["CurrentPage"] = opts.Page + doSearch := ctx.QueryBool("search") ctx.Data["search"] = doSearch @@ -79,6 +83,7 @@ func UnadoptedRepos(ctx *context.Context) { if !doSearch { pager := context.NewPagination(0, opts.PageSize, opts.Page, 5) pager.SetDefaultParams(ctx) + pager.AddParam(ctx, "search", "search") ctx.Data["Page"] = pager ctx.HTML(200, tplUnadoptedRepos) return @@ -92,6 +97,7 @@ func UnadoptedRepos(ctx *context.Context) { ctx.Data["Dirs"] = repoNames pager := context.NewPagination(int(count), opts.PageSize, opts.Page, 5) pager.SetDefaultParams(ctx) + pager.AddParam(ctx, "search", "search") ctx.Data["Page"] = pager ctx.HTML(200, tplUnadoptedRepos) } @@ -100,6 +106,9 @@ func UnadoptedRepos(ctx *context.Context) { func AdoptOrDeleteRepository(ctx *context.Context) { dir := ctx.Query("id") action := ctx.Query("action") + page := ctx.QueryInt("page") + q := ctx.Query("q") + dirSplit := strings.SplitN(dir, "/", 2) if len(dirSplit) != 2 { ctx.Redirect(setting.AppSubURL + "/admin/repos") @@ -148,5 +157,5 @@ func AdoptOrDeleteRepository(ctx *context.Context) { } ctx.Flash.Success(ctx.Tr("repo.delete_preexisting_success", dir)) } - ctx.Redirect(setting.AppSubURL + "/admin/repos/unadopted") + ctx.Redirect(setting.AppSubURL + "/admin/repos/unadopted?search=true&q=" + url.QueryEscape(q) + "&page=" + strconv.Itoa(page)) } diff --git a/templates/admin/repo/unadopted.tmpl b/templates/admin/repo/unadopted.tmpl index 206ae91f0515..bd47dd02625c 100644 --- a/templates/admin/repo/unadopted.tmpl +++ b/templates/admin/repo/unadopted.tmpl @@ -41,6 +41,8 @@ {{$.CsrfTokenHtml}} + +
{{svg "octicon-trashcan" 16 "mr-2"}} @@ -66,6 +68,8 @@ {{$.CsrfTokenHtml}} + +
{{svg "octicon-trashcan" 16 "mr-2"}} From bdeccc36886d6df4f2a8fce9e5f71e866e9902fc Mon Sep 17 00:00:00 2001 From: Norwin Date: Thu, 24 Dec 2020 18:14:01 +0000 Subject: [PATCH 08/46] Fix api doc response code (#14123) Fixes #14120 --- routers/api/v1/user/app.go | 2 +- templates/swagger/v1_json.tmpl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/routers/api/v1/user/app.go b/routers/api/v1/user/app.go index d02b8cea21e2..547730ea57e3 100644 --- a/routers/api/v1/user/app.go +++ b/routers/api/v1/user/app.go @@ -85,7 +85,7 @@ func CreateAccessToken(ctx *context.APIContext, form api.CreateAccessTokenOption // name: // type: string // responses: - // "200": + // "201": // "$ref": "#/responses/AccessToken" t := &models.AccessToken{ diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index d0303040c56f..72665e2b6d2d 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -11128,7 +11128,7 @@ } ], "responses": { - "200": { + "201": { "$ref": "#/responses/AccessToken" } } From 5a94db37ed16b253bf6e43ae6337f3a3c21f6d70 Mon Sep 17 00:00:00 2001 From: Daniil Pankratov Date: Thu, 24 Dec 2020 22:47:17 +0300 Subject: [PATCH 09/46] Fix creating OAuth2 auth source from CLI (#14116) Fix creation OAuth2 auth source from CLI. Fix #8356 Co-authored-by: Daniil Pankratov --- models/oauth2.go | 12 +++++++++++- modules/auth/oauth2/oauth2.go | 5 +++++ routers/user/auth.go | 13 +++++++++++-- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/models/oauth2.go b/models/oauth2.go index 65d62fdb617e..27668d5eecc6 100644 --- a/models/oauth2.go +++ b/models/oauth2.go @@ -125,8 +125,18 @@ func InitOAuth2() error { if err := oauth2.Init(x); err != nil { return err } - loginSources, _ := GetActiveOAuth2ProviderLoginSources() + return initOAuth2LoginSources() +} +// ResetOAuth2 clears existing OAuth2 providers and loads them from DB +func ResetOAuth2() error { + oauth2.ClearProviders() + return initOAuth2LoginSources() +} + +// initOAuth2LoginSources is used to load and register all active OAuth2 providers +func initOAuth2LoginSources() error { + loginSources, _ := GetActiveOAuth2ProviderLoginSources() for _, source := range loginSources { oAuth2Config := source.OAuth2() err := oauth2.RegisterProvider(source.Name, oAuth2Config.Provider, oAuth2Config.ClientID, oAuth2Config.ClientSecret, oAuth2Config.OpenIDConnectAutoDiscoveryURL, oAuth2Config.CustomURLMapping) diff --git a/modules/auth/oauth2/oauth2.go b/modules/auth/oauth2/oauth2.go index f69bc61d7549..e2c97b72f31d 100644 --- a/modules/auth/oauth2/oauth2.go +++ b/modules/auth/oauth2/oauth2.go @@ -119,6 +119,11 @@ func RemoveProvider(providerName string) { delete(goth.GetProviders(), providerName) } +// ClearProviders clears all OAuth2 providers from the goth lib +func ClearProviders() { + goth.ClearProviders() +} + // used to create different types of goth providers func createProvider(providerName, providerType, clientID, clientSecret, openIDConnectAutoDiscoveryURL string, customURLMapping *CustomURLMapping) (goth.Provider, error) { callbackURL := setting.AppURL + "user/oauth2/" + url.PathEscape(providerName) + "/callback" diff --git a/routers/user/auth.go b/routers/user/auth.go index acd88b364c3b..540a0d2f1a8e 100644 --- a/routers/user/auth.go +++ b/routers/user/auth.go @@ -570,8 +570,17 @@ func SignInOAuth(ctx *context.Context) { return } - err = oauth2.Auth(loginSource.Name, ctx.Req.Request, ctx.Resp) - if err != nil { + if err = oauth2.Auth(loginSource.Name, ctx.Req.Request, ctx.Resp); err != nil { + if strings.Contains(err.Error(), "no provider for ") { + if err = models.ResetOAuth2(); err != nil { + ctx.ServerError("SignIn", err) + return + } + if err = oauth2.Auth(loginSource.Name, ctx.Req.Request, ctx.Resp); err != nil { + ctx.ServerError("SignIn", err) + } + return + } ctx.ServerError("SignIn", err) } // redirect is done in oauth2.Auth From 2c8d302eb17e330624dad5f6cf1bcbf5ccf4917d Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 25 Dec 2020 04:58:30 +0800 Subject: [PATCH 10/46] Fix heatmap total contributions (#14141) --- web_src/js/components/ActivityHeatmap.vue | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/web_src/js/components/ActivityHeatmap.vue b/web_src/js/components/ActivityHeatmap.vue index 943bf704e281..7eb129d1397b 100644 --- a/web_src/js/components/ActivityHeatmap.vue +++ b/web_src/js/components/ActivityHeatmap.vue @@ -1,7 +1,7 @@