Skip to content

Commit 19b8e51

Browse files
committed
add session middleware and remove dependent on macaron for sso
1 parent 20484fa commit 19b8e51

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2161
-228
lines changed

go.mod

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.14
55
require (
66
code.gitea.io/gitea-vet v0.2.1
77
code.gitea.io/sdk/gitea v0.13.1
8+
gitea.com/go-chi/session v0.0.0-20201218134809-7209fa084f27
89
gitea.com/lunny/levelqueue v0.3.0
910
gitea.com/macaron/binding v0.0.0-20190822013154-a5f53841ed2b
1011
gitea.com/macaron/cache v0.0.0-20200924044943-905232fba10b
@@ -33,7 +34,7 @@ require (
3334
github.com/ethantkoenig/rupture v0.0.0-20181029165146-c3b3b810dc77
3435
github.com/gliderlabs/ssh v0.3.1
3536
github.com/glycerine/go-unsnap-stream v0.0.0-20190901134440-81cf024a9e0a // indirect
36-
github.com/go-chi/chi v1.5.0
37+
github.com/go-chi/chi v1.5.1
3738
github.com/go-enry/go-enry/v2 v2.6.0
3839
github.com/go-git/go-billy/v5 v5.0.0
3940
github.com/go-git/go-git/v5 v5.2.0
@@ -78,7 +79,6 @@ require (
7879
github.com/niklasfasching/go-org v1.3.2
7980
github.com/oliamb/cutter v0.2.2
8081
github.com/olivere/elastic/v7 v7.0.21
81-
github.com/onsi/ginkgo v1.13.0 // indirect
8282
github.com/pelletier/go-toml v1.8.1
8383
github.com/pierrec/lz4/v4 v4.1.1 // indirect
8484
github.com/pkg/errors v0.9.1
@@ -115,7 +115,6 @@ require (
115115
golang.org/x/text v0.3.4
116116
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e // indirect
117117
golang.org/x/tools v0.0.0-20201022035929-9cf592e881e9
118-
google.golang.org/appengine v1.6.7 // indirect
119118
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
120119
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
121120
gopkg.in/ini.v1 v1.62.0

go.sum

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ code.gitea.io/gitea-vet v0.2.1/go.mod h1:zcNbT/aJEmivCAhfmkHOlT645KNOf9W2KnkLgFj
4040
code.gitea.io/sdk/gitea v0.13.1 h1:Y7bpH2iO6Q0KhhMJfjP/LZ0AmiYITeRQlCD8b0oYqhk=
4141
code.gitea.io/sdk/gitea v0.13.1/go.mod h1:z3uwDV/b9Ls47NGukYM9XhnHtqPh/J+t40lsUrR6JDY=
4242
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
43+
gitea.com/go-chi/session v0.0.0-20201218134809-7209fa084f27 h1:cdb1OTNXGLwQ55gg+9tIPWufdsnrHWcIq8Qs+j/E8JU=
44+
gitea.com/go-chi/session v0.0.0-20201218134809-7209fa084f27/go.mod h1:Ozg8IchVNb/Udg+ui39iHRYqVHSvf3C99ixdpLR8Vu0=
4345
gitea.com/lunny/levelqueue v0.3.0 h1:MHn1GuSZkxvVEDMyAPqlc7A3cOW+q8RcGhRgH/xtm6I=
4446
gitea.com/lunny/levelqueue v0.3.0/go.mod h1:HBqmLbz56JWpfEGG0prskAV97ATNRoj5LDmPicD22hU=
4547
gitea.com/lunny/log v0.0.0-20190322053110-01b5df579c4e h1:r1en/D7xJmcY24VkHkjkcJFa+7ZWubVWPBrvsHkmHxk=
@@ -227,6 +229,8 @@ github.com/couchbase/go-couchbase v0.0.0-20201026062457-7b3be89bbd89/go.mod h1:+
227229
github.com/couchbase/gomemcached v0.0.0-20190515232915-c4b4ca0eb21d/go.mod h1:srVSlQLB8iXBVXHgnqemxUXqN6FCvClgCMPCsjBDR7c=
228230
github.com/couchbase/gomemcached v0.1.0 h1:whUde87n8CScx8ckMp2En5liqAlcuG3aKy/BQeBPu84=
229231
github.com/couchbase/gomemcached v0.1.0/go.mod h1:srVSlQLB8iXBVXHgnqemxUXqN6FCvClgCMPCsjBDR7c=
232+
github.com/couchbase/gomemcached v0.1.1 h1:xCS8ZglJDhrlQg3jmK7Rn1V8f7bPjXABLC05CgLQauc=
233+
github.com/couchbase/gomemcached v0.1.1/go.mod h1:mxliKQxOv84gQ0bJWbI+w9Wxdpt9HjDvgW9MjCym5Vo=
230234
github.com/couchbase/goutils v0.0.0-20190315194238-f9d42b11473b/go.mod h1:BQwMFlJzDjFDG3DJUdU0KORxn88UlsOULuxLExMh3Hs=
231235
github.com/couchbase/goutils v0.0.0-20201030094643-5e82bb967e67 h1:NCqJ6fwen6YP0WlV/IyibaT0kPt3JEI1rA62V/UPKT4=
232236
github.com/couchbase/goutils v0.0.0-20201030094643-5e82bb967e67/go.mod h1:BQwMFlJzDjFDG3DJUdU0KORxn88UlsOULuxLExMh3Hs=
@@ -264,6 +268,7 @@ github.com/denisenkom/go-mssqldb v0.9.0 h1:RSohk2RsiZqLZ0zCjtfn3S4Gp4exhpBWHyQ7D
264268
github.com/denisenkom/go-mssqldb v0.9.0/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
265269
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
266270
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
271+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
267272
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
268273
github.com/dlclark/regexp2 v1.1.6/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
269274
github.com/dlclark/regexp2 v1.2.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
@@ -328,8 +333,8 @@ github.com/glycerine/goconvey v0.0.0-20190410193231-58a59202ab31 h1:gclg6gY70GLy
328333
github.com/glycerine/goconvey v0.0.0-20190410193231-58a59202ab31/go.mod h1:Ogl1Tioa0aV7gstGFO7KhffUsb9M4ydbEbbxpcEDc24=
329334
github.com/go-asn1-ber/asn1-ber v1.5.1 h1:pDbRAunXzIUXfx4CB2QJFv5IuPiuoW+sWvr/Us009o8=
330335
github.com/go-asn1-ber/asn1-ber v1.5.1/go.mod h1:hEBeB/ic+5LoWskz+yKT7vGhhPYkProFKoKdwZRWMe0=
331-
github.com/go-chi/chi v1.5.0 h1:2ZcJZozJ+rj6BA0c19ffBUGXEKAT/aOLOtQjD46vBRA=
332-
github.com/go-chi/chi v1.5.0/go.mod h1:REp24E+25iKvxgeTfHmdUoL5x15kBiDBlnIl5bCwe2k=
336+
github.com/go-chi/chi v1.5.1 h1:kfTK3Cxd/dkMu/rKs5ZceWYp+t5CtiE7vmaTv3LjC6w=
337+
github.com/go-chi/chi v1.5.1/go.mod h1:REp24E+25iKvxgeTfHmdUoL5x15kBiDBlnIl5bCwe2k=
333338
github.com/go-enry/go-enry/v2 v2.6.0 h1:nbGWQBpO+D+cJuRxNgSDFnFY9QWz3QM/CeZxU7VAH20=
334339
github.com/go-enry/go-enry/v2 v2.6.0/go.mod h1:GVzIiAytiS5uT/QiuakK7TF1u4xDab87Y8V5EJRpsIQ=
335340
github.com/go-enry/go-oniguruma v1.2.1 h1:k8aAMuJfMrqm/56SG2lV9Cfti6tC4x8673aHCcBk+eo=
@@ -424,6 +429,7 @@ github.com/go-redis/redis v6.15.2+incompatible h1:9SpNVG76gr6InJGxoZ6IuuxaCOQwDA
424429
github.com/go-redis/redis v6.15.2+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
425430
github.com/go-redis/redis/v7 v7.4.0 h1:7obg6wUoj05T0EpY0o8B59S9w5yeMWql7sw2kwNW1x4=
426431
github.com/go-redis/redis/v7 v7.4.0/go.mod h1:JDNMw23GTyLNC4GZu9njt15ctBQVn7xjRfnwdHj/Dcg=
432+
github.com/go-redis/redis/v8 v8.4.0/go.mod h1:A1tbYoHSa1fXwN+//ljcCYYJeLmVrwL9hbQN45Jdy0M=
427433
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
428434
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
429435
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
@@ -521,6 +527,7 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
521527
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
522528
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
523529
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
530+
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
524531
github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
525532
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
526533
github.com/google/go-github/v32 v32.1.0 h1:GWkQOdXqviCPx7Q7Fj+KyPoGm4SwHRh8rheoPhd27II=
@@ -873,15 +880,17 @@ github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+W
873880
github.com/onsi/ginkgo v1.10.1 h1:q/mM8GF/n0shIN8SaAZ0V+jnLPzen6WIVZdiwrRlMlo=
874881
github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
875882
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
876-
github.com/onsi/ginkgo v1.13.0 h1:M76yO2HkZASFjXL0HSoZJ1AYEmQxNJmY41Jx1zNUq1Y=
877-
github.com/onsi/ginkgo v1.13.0/go.mod h1:+REjRxOmWfHCjfv9TTWB1jD1Frx4XydAD3zm1lskyM0=
883+
github.com/onsi/ginkgo v1.14.2 h1:8mVmC9kjFFmA8H4pKMUhcblgifdkOIXPvbhN1T36q1M=
884+
github.com/onsi/ginkgo v1.14.2/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY=
878885
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
879886
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
880887
github.com/onsi/gomega v1.7.0 h1:XPnZz8VVBHjVsy1vzJmRwIcSwiUO+JFfrv/xGiigmME=
881888
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
882889
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
883890
github.com/onsi/gomega v1.10.1 h1:o0+MgICZLuZ7xjH7Vx6zS/zcu93/BEp1VwkIW1mEXCE=
884891
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
892+
github.com/onsi/gomega v1.10.3 h1:gph6h/qe9GSUw1NhH1gp+qb+h8rXD8Cy60Z32Qw3ELA=
893+
github.com/onsi/gomega v1.10.3/go.mod h1:V9xEwhxec5O8UDM77eCW8vLymOMltsqPVYWrpDsH8xc=
885894
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk=
886895
github.com/opentracing-contrib/go-observer v0.0.0-20170622124052-a52f23424492/go.mod h1:Ngi6UdF0k5OKD5t5wlmGhe/EDKPoUM3BXZSSfIuJbis=
887896
github.com/opentracing/basictracer-go v1.0.0/go.mod h1:QfBfYuafItcjQuMwinw9GhYKwFXS9KnPs5lxoYwgW74=
@@ -979,7 +988,6 @@ github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD
979988
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
980989
github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E=
981990
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
982-
github.com/sclevine/agouti v3.0.0+incompatible/go.mod h1:b4WX9W9L1sfQKXeJf1mUTLZKJ48R1S7H23Ji7oFO5Bw=
983991
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
984992
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
985993
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
@@ -1146,6 +1154,7 @@ go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
11461154
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
11471155
go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
11481156
go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
1157+
go.opentelemetry.io/otel v0.14.0/go.mod h1:vH5xEuwy7Rts0GNtsCW3HYQoZDY+OmBJ6t1bFGGlxgw=
11491158
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
11501159
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
11511160
go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
@@ -1262,6 +1271,7 @@ golang.org/x/net v0.0.0-20200927032502-5d4f70055728/go.mod h1:/O7V0waA8r7cgGh81R
12621271
golang.org/x/net v0.0.0-20200930145003-4acb6c075d10 h1:YfxMZzv3PjGonQYNUaeU2+DhAdqOxerQ30JFB6WgAXo=
12631272
golang.org/x/net v0.0.0-20200930145003-4acb6c075d10/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
12641273
golang.org/x/net v0.0.0-20200930145003-4acb6c075d10/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
1274+
golang.org/x/net v0.0.0-20201006153459-a7d1128ccaa0/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
12651275
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
12661276
golang.org/x/net v0.0.0-20201031054903-ff519b6c9102 h1:42cLlJJdEh+ySyeUUbEQ5bsTiq8voBeTuweGVkY6Puw=
12671277
golang.org/x/net v0.0.0-20201031054903-ff519b6c9102/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=

modules/auth/auth.go

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@ import (
99
"reflect"
1010
"strings"
1111

12-
"code.gitea.io/gitea/models"
13-
"code.gitea.io/gitea/modules/auth/sso"
1412
"code.gitea.io/gitea/modules/validation"
1513

1614
"gitea.com/macaron/binding"
1715
"gitea.com/macaron/macaron"
18-
"gitea.com/macaron/session"
1916
"github.com/unknwon/com"
2017
)
2118

@@ -24,28 +21,6 @@ func IsAPIPath(url string) bool {
2421
return strings.HasPrefix(url, "/api/")
2522
}
2623

27-
// SignedInUser returns the user object of signed user.
28-
// It returns a bool value to indicate whether user uses basic auth or not.
29-
func SignedInUser(ctx *macaron.Context, sess session.Store) (*models.User, bool) {
30-
if !models.HasEngine {
31-
return nil, false
32-
}
33-
34-
// Try to sign in with each of the enabled plugins
35-
for _, ssoMethod := range sso.Methods() {
36-
if !ssoMethod.IsEnabled() {
37-
continue
38-
}
39-
user := ssoMethod.VerifyAuthData(ctx, sess)
40-
if user != nil {
41-
_, isBasic := ssoMethod.(*sso.Basic)
42-
return user, isBasic
43-
}
44-
}
45-
46-
return nil, false
47-
}
48-
4924
// Form form binding interface
5025
type Form interface {
5126
binding.Validator

modules/auth/sso/basic.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@
66
package sso
77

88
import (
9+
"net/http"
910
"strings"
1011

1112
"code.gitea.io/gitea/models"
1213
"code.gitea.io/gitea/modules/base"
1314
"code.gitea.io/gitea/modules/log"
1415
"code.gitea.io/gitea/modules/setting"
1516
"code.gitea.io/gitea/modules/timeutil"
16-
17-
"gitea.com/macaron/macaron"
18-
"gitea.com/macaron/session"
1917
)
2018

2119
// Ensure the struct implements the interface.
@@ -49,8 +47,8 @@ func (b *Basic) IsEnabled() bool {
4947
// "Authorization" header of the request and returns the corresponding user object for that
5048
// name/token on successful validation.
5149
// Returns nil if header is empty or validation fails.
52-
func (b *Basic) VerifyAuthData(ctx *macaron.Context, sess session.Store) *models.User {
53-
baHead := ctx.Req.Header.Get("Authorization")
50+
func (b *Basic) VerifyAuthData(req *http.Request, store DataStore, sess SessionStore) *models.User {
51+
baHead := req.Header.Get("Authorization")
5452
if len(baHead) == 0 {
5553
return nil
5654
}
@@ -75,7 +73,7 @@ func (b *Basic) VerifyAuthData(ctx *macaron.Context, sess session.Store) *models
7573
uid := CheckOAuthAccessToken(authToken)
7674
if uid != 0 {
7775
var err error
78-
ctx.Data["IsApiToken"] = true
76+
store.GetData()["IsApiToken"] = true
7977

8078
u, err = models.GetUserByID(uid)
8179
if err != nil {
@@ -108,7 +106,7 @@ func (b *Basic) VerifyAuthData(ctx *macaron.Context, sess session.Store) *models
108106
return nil
109107
}
110108
} else {
111-
ctx.Data["IsApiToken"] = true
109+
store.GetData()["IsApiToken"] = true
112110
}
113111

114112
return u

modules/auth/sso/interface.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,23 @@
55
package sso
66

77
import (
8-
"code.gitea.io/gitea/models"
8+
"net/http"
99

10-
"gitea.com/macaron/macaron"
11-
"gitea.com/macaron/session"
10+
"code.gitea.io/gitea/models"
1211
)
1312

13+
// DataStore represents a data store
14+
type DataStore interface {
15+
GetData() map[string]interface{}
16+
}
17+
18+
// SessionStore represents a session store
19+
type SessionStore interface {
20+
Get(interface{}) interface{}
21+
Set(interface{}, interface{}) error
22+
Delete(interface{}) error
23+
}
24+
1425
// SingleSignOn represents a SSO authentication method (plugin) for HTTP requests.
1526
type SingleSignOn interface {
1627
// Init should be called exactly once before using any of the other methods,
@@ -29,5 +40,5 @@ type SingleSignOn interface {
2940
// or a new user object (with id = 0) populated with the information that was found
3041
// in the authentication data (username or email).
3142
// Returns nil if verification fails.
32-
VerifyAuthData(ctx *macaron.Context, sess session.Store) *models.User
43+
VerifyAuthData(http *http.Request, store DataStore, sess SessionStore) *models.User
3344
}

modules/auth/sso/oauth2.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@
66
package sso
77

88
import (
9+
"net/http"
910
"strings"
1011
"time"
1112

1213
"code.gitea.io/gitea/models"
1314
"code.gitea.io/gitea/modules/log"
1415
"code.gitea.io/gitea/modules/timeutil"
15-
16-
"gitea.com/macaron/macaron"
17-
"gitea.com/macaron/session"
1816
)
1917

2018
// Ensure the struct implements the interface.
@@ -63,15 +61,15 @@ func (o *OAuth2) Free() error {
6361
}
6462

6563
// userIDFromToken returns the user id corresponding to the OAuth token.
66-
func (o *OAuth2) userIDFromToken(ctx *macaron.Context) int64 {
64+
func (o *OAuth2) userIDFromToken(req *http.Request, store DataStore) int64 {
6765
// Check access token.
68-
tokenSHA := ctx.Query("token")
66+
tokenSHA := req.Form.Get("token")
6967
if len(tokenSHA) == 0 {
70-
tokenSHA = ctx.Query("access_token")
68+
tokenSHA = req.Form.Get("access_token")
7169
}
7270
if len(tokenSHA) == 0 {
7371
// Well, check with header again.
74-
auHead := ctx.Req.Header.Get("Authorization")
72+
auHead := req.Header.Get("Authorization")
7573
if len(auHead) > 0 {
7674
auths := strings.Fields(auHead)
7775
if len(auths) == 2 && (auths[0] == "token" || strings.ToLower(auths[0]) == "bearer") {
@@ -87,7 +85,7 @@ func (o *OAuth2) userIDFromToken(ctx *macaron.Context) int64 {
8785
if strings.Contains(tokenSHA, ".") {
8886
uid := CheckOAuthAccessToken(tokenSHA)
8987
if uid != 0 {
90-
ctx.Data["IsApiToken"] = true
88+
store.GetData()["IsApiToken"] = true
9189
}
9290
return uid
9391
}
@@ -102,7 +100,7 @@ func (o *OAuth2) userIDFromToken(ctx *macaron.Context) int64 {
102100
if err = models.UpdateAccessToken(t); err != nil {
103101
log.Error("UpdateAccessToken: %v", err)
104102
}
105-
ctx.Data["IsApiToken"] = true
103+
store.GetData()["IsApiToken"] = true
106104
return t.UID
107105
}
108106

@@ -116,16 +114,16 @@ func (o *OAuth2) IsEnabled() bool {
116114
// or the "Authorization" header and returns the corresponding user object for that ID.
117115
// If verification is successful returns an existing user object.
118116
// Returns nil if verification fails.
119-
func (o *OAuth2) VerifyAuthData(ctx *macaron.Context, sess session.Store) *models.User {
117+
func (o *OAuth2) VerifyAuthData(req *http.Request, store DataStore, sess SessionStore) *models.User {
120118
if !models.HasEngine {
121119
return nil
122120
}
123121

124-
if isInternalPath(ctx) || !isAPIPath(ctx) && !isAttachmentDownload(ctx) {
122+
if isInternalPath(req) || !isAPIPath(req) && !isAttachmentDownload(req) {
125123
return nil
126124
}
127125

128-
id := o.userIDFromToken(ctx)
126+
id := o.userIDFromToken(req, store)
129127
if id <= 0 {
130128
return nil
131129
}

modules/auth/sso/reverseproxy.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
package sso
77

88
import (
9+
"net/http"
910
"strings"
1011

1112
"code.gitea.io/gitea/models"
1213
"code.gitea.io/gitea/modules/log"
1314
"code.gitea.io/gitea/modules/setting"
1415

15-
"gitea.com/macaron/macaron"
16-
"gitea.com/macaron/session"
1716
gouuid "github.com/google/uuid"
1817
)
1918

@@ -31,8 +30,8 @@ type ReverseProxy struct {
3130
}
3231

3332
// getUserName extracts the username from the "setting.ReverseProxyAuthUser" header
34-
func (r *ReverseProxy) getUserName(ctx *macaron.Context) string {
35-
webAuthUser := strings.TrimSpace(ctx.Req.Header.Get(setting.ReverseProxyAuthUser))
33+
func (r *ReverseProxy) getUserName(req *http.Request) string {
34+
webAuthUser := strings.TrimSpace(req.Header.Get(setting.ReverseProxyAuthUser))
3635
if len(webAuthUser) == 0 {
3736
return ""
3837
}
@@ -61,16 +60,16 @@ func (r *ReverseProxy) IsEnabled() bool {
6160
// If a username is available in the "setting.ReverseProxyAuthUser" header an existing
6261
// user object is returned (populated with username or email found in header).
6362
// Returns nil if header is empty.
64-
func (r *ReverseProxy) VerifyAuthData(ctx *macaron.Context, sess session.Store) *models.User {
65-
username := r.getUserName(ctx)
63+
func (r *ReverseProxy) VerifyAuthData(req *http.Request, store DataStore, sess SessionStore) *models.User {
64+
username := r.getUserName(req)
6665
if len(username) == 0 {
6766
return nil
6867
}
6968

7069
user, err := models.GetUserByName(username)
7170
if err != nil {
7271
if models.IsErrUserNotExist(err) && r.isAutoRegisterAllowed() {
73-
return r.newUser(ctx)
72+
return r.newUser(req)
7473
}
7574
log.Error("GetUserByName: %v", err)
7675
return nil
@@ -86,15 +85,15 @@ func (r *ReverseProxy) isAutoRegisterAllowed() bool {
8685

8786
// newUser creates a new user object for the purpose of automatic registration
8887
// and populates its name and email with the information present in request headers.
89-
func (r *ReverseProxy) newUser(ctx *macaron.Context) *models.User {
90-
username := r.getUserName(ctx)
88+
func (r *ReverseProxy) newUser(req *http.Request) *models.User {
89+
username := r.getUserName(req)
9190
if len(username) == 0 {
9291
return nil
9392
}
9493

9594
email := gouuid.New().String() + "@localhost"
9695
if setting.Service.EnableReverseProxyEmail {
97-
webAuthEmail := ctx.Req.Header.Get(setting.ReverseProxyAuthEmail)
96+
webAuthEmail := req.Header.Get(setting.ReverseProxyAuthEmail)
9897
if len(webAuthEmail) > 0 {
9998
email = webAuthEmail
10099
}

0 commit comments

Comments
 (0)