-
Notifications
You must be signed in to change notification settings - Fork 16
/
gitee.go
50 lines (43 loc) · 1.26 KB
/
gitee.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright 2017 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gitee
import (
"net/http"
"strings"
"github.com/drone/go-login/login"
"github.com/drone/go-login/login/internal/oauth2"
)
var _ login.Middleware = (*Config)(nil)
// Config configures the Gitee auth provider.
type Config struct {
ClientID string
ClientSecret string
RedirectURL string
Server string
Scope []string
Client *http.Client
}
// Handler returns a http.Handler that runs h at the
// completion of the Gitee authorization flow. The Gitee
// authorization details are available to h in the
// http.Request context.
func (c *Config) Handler(h http.Handler) http.Handler {
server := normalizeAddress(c.Server)
return oauth2.Handler(h, &oauth2.Config{
BasicAuthOff: true,
Client: c.Client,
ClientID: c.ClientID,
ClientSecret: c.ClientSecret,
RedirectURL: c.RedirectURL,
AccessTokenURL: server + "/oauth/token",
AuthorizationURL: server + "/oauth/authorize",
Scope: c.Scope,
})
}
func normalizeAddress(address string) string {
if address == "" {
return "https://gitee.com"
}
return strings.TrimSuffix(address, "/")
}