5
5
package sso
6
6
7
7
import (
8
+ "code.gitea.io/gitea/modules/templates"
8
9
"errors"
9
10
"net/http"
10
- "reflect"
11
11
"strings"
12
12
13
13
"code.gitea.io/gitea/models"
14
14
"code.gitea.io/gitea/modules/base"
15
15
"code.gitea.io/gitea/modules/log"
16
16
"code.gitea.io/gitea/modules/setting"
17
17
18
- "gitea.com/macaron/macaron"
19
- "gitea.com/macaron/session"
20
-
21
18
gouuid "github.com/google/uuid"
22
19
"github.com/quasoft/websspi"
20
+ "github.com/unrolled/render"
23
21
)
24
22
25
23
const (
@@ -65,8 +63,8 @@ func (s *SSPI) IsEnabled() bool {
65
63
// If authentication is successful, returs the corresponding user object.
66
64
// If negotiation should continue or authentication fails, immediately returns a 401 HTTP
67
65
// response code, as required by the SPNEGO protocol.
68
- func (s * SSPI ) VerifyAuthData (req * http.Request , store DataStore , sess SessionStore ) * models.User {
69
- if ! s .shouldAuthenticate (ctx ) {
66
+ func (s * SSPI ) VerifyAuthData (req * http.Request , w http. ResponseWriter , store DataStore , sess SessionStore ) * models.User {
67
+ if ! s .shouldAuthenticate (req ) {
70
68
return nil
71
69
}
72
70
@@ -76,22 +74,36 @@ func (s *SSPI) VerifyAuthData(req *http.Request, store DataStore, sess SessionSt
76
74
return nil
77
75
}
78
76
79
- userInfo , outToken , err := sspiAuth .Authenticate (req , ctx . Resp )
77
+ userInfo , outToken , err := sspiAuth .Authenticate (req , w )
80
78
if err != nil {
81
79
log .Warn ("Authentication failed with error: %v\n " , err )
82
- sspiAuth .AppendAuthenticateHeader (ctx . Resp , outToken )
80
+ sspiAuth .AppendAuthenticateHeader (w , outToken )
83
81
84
82
// Include the user login page in the 401 response to allow the user
85
83
// to login with another authentication method if SSPI authentication
86
84
// fails
87
- addFlashErr (ctx , ctx .Tr ("auth.sspi_auth_failed" ))
88
- ctx .Data ["EnableOpenIDSignIn" ] = setting .Service .EnableOpenIDSignIn
89
- ctx .Data ["EnableSSPI" ] = true
90
- ctx .HTML (401 , string (tplSignIn ))
85
+ //addFlashErr(ctx, ctx.Tr("auth.sspi_auth_failed"))
86
+
87
+ store .GetData ()["EnableOpenIDSignIn" ] = setting .Service .EnableOpenIDSignIn
88
+ store .GetData ()["EnableSSPI" ] = true
89
+
90
+ rnd := render .New (render.Options {
91
+ Extensions : []string {".tmpl" },
92
+ Directory : "templates" ,
93
+ Funcs : templates .NewFuncMap (),
94
+ Asset : templates .GetAsset ,
95
+ AssetNames : templates .GetAssetNames ,
96
+ IsDevelopment : setting .RunMode != "prod" ,
97
+ })
98
+ err := rnd .HTML (w , 401 , string (tplSignIn ), templates .BaseVars ().Merge (store .GetData ()))
99
+ if err != nil {
100
+ log .Error ("%v" , err )
101
+ }
102
+
91
103
return nil
92
104
}
93
105
if outToken != "" {
94
- sspiAuth .AppendAuthenticateHeader (ctx . Resp , outToken )
106
+ sspiAuth .AppendAuthenticateHeader (w , outToken )
95
107
}
96
108
97
109
username := sanitizeUsername (userInfo .Username , cfg )
@@ -110,16 +122,16 @@ func (s *SSPI) VerifyAuthData(req *http.Request, store DataStore, sess SessionSt
110
122
log .Error ("User '%s' not found" , username )
111
123
return nil
112
124
}
113
- user , err = s .newUser (ctx , username , cfg )
125
+ user , err = s .newUser (username , cfg )
114
126
if err != nil {
115
127
log .Error ("CreateUser: %v" , err )
116
128
return nil
117
129
}
118
130
}
119
131
120
132
// Make sure requests to API paths and PWA resources do not create a new session
121
- if ! isAPIPath (ctx ) && ! isAttachmentDownload (ctx ) {
122
- handleSignIn (ctx , sess , user )
133
+ if ! isAPIPath (req ) && ! isAttachmentDownload (req ) {
134
+ handleSignIn (w , req , sess , user )
123
135
}
124
136
125
137
return user
@@ -146,7 +158,7 @@ func (s *SSPI) shouldAuthenticate(req *http.Request) (shouldAuth bool) {
146
158
if path == "/user/login" {
147
159
if req .FormValue ("user_name" ) != "" && req .FormValue ("password" ) != "" {
148
160
shouldAuth = false
149
- } else if ctx . Req .FormValue ("auth_with_sspi" ) == "1" {
161
+ } else if req .FormValue ("auth_with_sspi" ) == "1" {
150
162
shouldAuth = true
151
163
}
152
164
} else if isInternalPath (req ) {
@@ -217,6 +229,8 @@ func sanitizeUsername(username string, cfg *models.SSPIConfig) string {
217
229
return username
218
230
}
219
231
232
+ /*
233
+ // TODO flash err not implemented for chi
220
234
// addFlashErr adds an error message to the Flash object mapped to a macaron.Context
221
235
func addFlashErr(ctx *macaron.Context, err string) {
222
236
fv := ctx.GetVal(reflect.TypeOf(&session.Flash{}))
@@ -231,6 +245,8 @@ func addFlashErr(ctx *macaron.Context, err string) {
231
245
ctx.Data["Flash"] = flash
232
246
}
233
247
248
+ */
249
+
234
250
// init registers the SSPI auth method as the last method in the list.
235
251
// The SSPI plugin is expected to be executed last, as it returns 401 status code if negotiation
236
252
// fails (or if negotiation should continue), which would prevent other authentication methods
0 commit comments