Skip to content

Commit a1c9e8f

Browse files
lunny6543
andauthored
Fix windows build error (#14263)
* fix build * take flash error message back and fix more windows lint error * performance optimization * own step to check lint for windows Co-authored-by: 6543 <[email protected]>
1 parent 4ef5f17 commit a1c9e8f

File tree

12 files changed

+61
-44
lines changed

12 files changed

+61
-44
lines changed

.drone.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ steps:
3333
GOSUMDB: sum.golang.org
3434
TAGS: bindata sqlite sqlite_unlock_notify
3535

36+
- name: lint-backend-windows
37+
pull: always
38+
image: golang:1.15
39+
commands:
40+
- make golangci-lint vet
41+
environment:
42+
GOPROXY: https://goproxy.cn # proxy.golang.org is blocked in China, this proxy is not
43+
GOSUMDB: sum.golang.org
44+
TAGS: bindata sqlite sqlite_unlock_notify
45+
GOOS: windows
46+
GOARCH: amd64
47+
3648
- name: lint-backend-gogit
3749
pull: always
3850
image: golang:1.15

modules/auth/sso/basic.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (b *Basic) IsEnabled() bool {
4747
// "Authorization" header of the request and returns the corresponding user object for that
4848
// name/token on successful validation.
4949
// Returns nil if header is empty or validation fails.
50-
func (b *Basic) VerifyAuthData(req *http.Request, store DataStore, sess SessionStore) *models.User {
50+
func (b *Basic) VerifyAuthData(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) *models.User {
5151
baHead := req.Header.Get("Authorization")
5252
if len(baHead) == 0 {
5353
return nil

modules/auth/sso/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,5 @@ type SingleSignOn interface {
4040
// or a new user object (with id = 0) populated with the information that was found
4141
// in the authentication data (username or email).
4242
// Returns nil if verification fails.
43-
VerifyAuthData(http *http.Request, store DataStore, sess SessionStore) *models.User
43+
VerifyAuthData(http *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) *models.User
4444
}

modules/auth/sso/oauth2.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func (o *OAuth2) IsEnabled() bool {
114114
// or the "Authorization" header and returns the corresponding user object for that ID.
115115
// If verification is successful returns an existing user object.
116116
// Returns nil if verification fails.
117-
func (o *OAuth2) VerifyAuthData(req *http.Request, store DataStore, sess SessionStore) *models.User {
117+
func (o *OAuth2) VerifyAuthData(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) *models.User {
118118
if !models.HasEngine {
119119
return nil
120120
}

modules/auth/sso/reverseproxy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (r *ReverseProxy) IsEnabled() bool {
6060
// If a username is available in the "setting.ReverseProxyAuthUser" header an existing
6161
// user object is returned (populated with username or email found in header).
6262
// Returns nil if header is empty.
63-
func (r *ReverseProxy) VerifyAuthData(req *http.Request, store DataStore, sess SessionStore) *models.User {
63+
func (r *ReverseProxy) VerifyAuthData(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) *models.User {
6464
username := r.getUserName(req)
6565
if len(username) == 0 {
6666
return nil

modules/auth/sso/session.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (s *Session) IsEnabled() bool {
3939
// VerifyAuthData checks if there is a user uid stored in the session and returns the user
4040
// object for that uid.
4141
// Returns nil if there is no user uid stored in the session.
42-
func (s *Session) VerifyAuthData(req *http.Request, store DataStore, sess SessionStore) *models.User {
42+
func (s *Session) VerifyAuthData(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) *models.User {
4343
user := SessionUser(sess)
4444
if user != nil {
4545
return user

modules/auth/sso/sspi_windows.go

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,17 @@ package sso
77
import (
88
"errors"
99
"net/http"
10-
"reflect"
1110
"strings"
1211

1312
"code.gitea.io/gitea/models"
1413
"code.gitea.io/gitea/modules/base"
1514
"code.gitea.io/gitea/modules/log"
1615
"code.gitea.io/gitea/modules/setting"
17-
18-
"gitea.com/macaron/macaron"
19-
"gitea.com/macaron/session"
16+
"code.gitea.io/gitea/modules/templates"
2017

2118
gouuid "github.com/google/uuid"
2219
"github.com/quasoft/websspi"
20+
"github.com/unrolled/render"
2321
)
2422

2523
const (
@@ -41,14 +39,26 @@ var (
4139
// On successful authentication returns a valid user object.
4240
// Returns nil if authentication fails.
4341
type SSPI struct {
42+
rnd *render.Render
4443
}
4544

4645
// Init creates a new global websspi.Authenticator object
4746
func (s *SSPI) Init() error {
4847
config := websspi.NewConfig()
4948
var err error
5049
sspiAuth, err = websspi.New(config)
51-
return err
50+
if err != nil {
51+
return err
52+
}
53+
s.rnd = render.New(render.Options{
54+
Extensions: []string{".tmpl"},
55+
Directory: "templates",
56+
Funcs: templates.NewFuncMap(),
57+
Asset: templates.GetAsset,
58+
AssetNames: templates.GetAssetNames,
59+
IsDevelopment: setting.RunMode != "prod",
60+
})
61+
return nil
5262
}
5363

5464
// Free releases resources used by the global websspi.Authenticator object
@@ -65,8 +75,8 @@ func (s *SSPI) IsEnabled() bool {
6575
// If authentication is successful, returs the corresponding user object.
6676
// If negotiation should continue or authentication fails, immediately returns a 401 HTTP
6777
// 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) {
78+
func (s *SSPI) VerifyAuthData(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) *models.User {
79+
if !s.shouldAuthenticate(req) {
7080
return nil
7181
}
7282

@@ -76,22 +86,29 @@ func (s *SSPI) VerifyAuthData(req *http.Request, store DataStore, sess SessionSt
7686
return nil
7787
}
7888

79-
userInfo, outToken, err := sspiAuth.Authenticate(req, ctx.Resp)
89+
userInfo, outToken, err := sspiAuth.Authenticate(req, w)
8090
if err != nil {
8191
log.Warn("Authentication failed with error: %v\n", err)
82-
sspiAuth.AppendAuthenticateHeader(ctx.Resp, outToken)
92+
sspiAuth.AppendAuthenticateHeader(w, outToken)
8393

8494
// Include the user login page in the 401 response to allow the user
8595
// to login with another authentication method if SSPI authentication
8696
// 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))
97+
store.GetData()["Flash"] = map[string]string{
98+
"ErrMsg": err.Error(),
99+
}
100+
store.GetData()["EnableOpenIDSignIn"] = setting.Service.EnableOpenIDSignIn
101+
store.GetData()["EnableSSPI"] = true
102+
103+
err := s.rnd.HTML(w, 401, string(tplSignIn), templates.BaseVars().Merge(store.GetData()))
104+
if err != nil {
105+
log.Error("%v", err)
106+
}
107+
91108
return nil
92109
}
93110
if outToken != "" {
94-
sspiAuth.AppendAuthenticateHeader(ctx.Resp, outToken)
111+
sspiAuth.AppendAuthenticateHeader(w, outToken)
95112
}
96113

97114
username := sanitizeUsername(userInfo.Username, cfg)
@@ -110,16 +127,16 @@ func (s *SSPI) VerifyAuthData(req *http.Request, store DataStore, sess SessionSt
110127
log.Error("User '%s' not found", username)
111128
return nil
112129
}
113-
user, err = s.newUser(ctx, username, cfg)
130+
user, err = s.newUser(username, cfg)
114131
if err != nil {
115132
log.Error("CreateUser: %v", err)
116133
return nil
117134
}
118135
}
119136

120137
// 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)
138+
if !isAPIPath(req) && !isAttachmentDownload(req) {
139+
handleSignIn(w, req, sess, user)
123140
}
124141

125142
return user
@@ -146,7 +163,7 @@ func (s *SSPI) shouldAuthenticate(req *http.Request) (shouldAuth bool) {
146163
if path == "/user/login" {
147164
if req.FormValue("user_name") != "" && req.FormValue("password") != "" {
148165
shouldAuth = false
149-
} else if ctx.Req.FormValue("auth_with_sspi") == "1" {
166+
} else if req.FormValue("auth_with_sspi") == "1" {
150167
shouldAuth = true
151168
}
152169
} else if isInternalPath(req) {
@@ -217,20 +234,6 @@ func sanitizeUsername(username string, cfg *models.SSPIConfig) string {
217234
return username
218235
}
219236

220-
// addFlashErr adds an error message to the Flash object mapped to a macaron.Context
221-
func addFlashErr(ctx *macaron.Context, err string) {
222-
fv := ctx.GetVal(reflect.TypeOf(&session.Flash{}))
223-
if !fv.IsValid() {
224-
return
225-
}
226-
flash, ok := fv.Interface().(*session.Flash)
227-
if !ok {
228-
return
229-
}
230-
flash.Error(err)
231-
ctx.Data["Flash"] = flash
232-
}
233-
234237
// init registers the SSPI auth method as the last method in the list.
235238
// The SSPI plugin is expected to be executed last, as it returns 401 status code if negotiation
236239
// fails (or if negotiation should continue), which would prevent other authentication methods

modules/auth/sso/user.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
// SignedInUser returns the user object of signed user.
1414
// It returns a bool value to indicate whether user uses basic auth or not.
15-
func SignedInUser(req *http.Request, ds DataStore, sess SessionStore) (*models.User, bool) {
15+
func SignedInUser(req *http.Request, w http.ResponseWriter, ds DataStore, sess SessionStore) (*models.User, bool) {
1616
if !models.HasEngine {
1717
return nil, false
1818
}
@@ -22,7 +22,7 @@ func SignedInUser(req *http.Request, ds DataStore, sess SessionStore) (*models.U
2222
if !ssoMethod.IsEnabled() {
2323
continue
2424
}
25-
user := ssoMethod.VerifyAuthData(req, ds, sess)
25+
user := ssoMethod.VerifyAuthData(req, w, ds, sess)
2626
if user != nil {
2727
_, isBasic := ssoMethod.(*Basic)
2828
return user, isBasic

modules/context/context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ func Contexter() macaron.Handler {
309309
}
310310

311311
// Get user from session if logged in.
312-
ctx.User, ctx.IsBasicAuth = sso.SignedInUser(ctx.Req.Request, ctx, ctx.Session)
312+
ctx.User, ctx.IsBasicAuth = sso.SignedInUser(ctx.Req.Request, c.Resp, ctx, ctx.Session)
313313

314314
if ctx.User != nil {
315315
ctx.IsSigned = true

modules/graceful/manager_windows.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,17 @@ func (g *Manager) start() {
7373

7474
// Make SVC process
7575
run := svc.Run
76-
isInteractive, err := svc.IsAnInteractiveSession()
76+
isInteractive, err := svc.IsWindowsService()
7777
if err != nil {
7878
log.Error("Unable to ascertain if running as an Interactive Session: %v", err)
7979
return
8080
}
8181
if isInteractive {
8282
run = debug.Run
8383
}
84-
go run(WindowsServiceName, g)
84+
go func() {
85+
_ = run(WindowsServiceName, g)
86+
}()
8587
}
8688

8789
// Execute makes Manager implement svc.Handler

modules/log/console_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func enableVTMode(console windows.Handle) bool {
2323
// https://docs.microsoft.com/en-us/windows/console/setconsolemode
2424
// It only works on windows 10. Earlier terminals will fail with an err which we will
2525
// handle to say don't color
26-
mode = mode | windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING
26+
mode |= windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING
2727
err = windows.SetConsoleMode(console, mode)
2828
return err == nil
2929
}

routers/routes/recovery.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func Recovery() func(next http.Handler) http.Handler {
7575
}
7676

7777
// Get user from session if logged in.
78-
user, _ := sso.SignedInUser(req, &store, sess)
78+
user, _ := sso.SignedInUser(req, w, &store, sess)
7979
if user != nil {
8080
store.Data["IsSigned"] = true
8181
store.Data["SignedUser"] = user

0 commit comments

Comments
 (0)