diff --git a/example/main.go b/example/main.go index aefa77d..3f4c973 100644 --- a/example/main.go +++ b/example/main.go @@ -15,6 +15,7 @@ import ( "github.com/drone/go-login/login/bitbucket" "github.com/drone/go-login/login/github" "github.com/drone/go-login/login/gitlab" + "github.com/drone/go-login/login/gitee" "github.com/drone/go-login/login/gogs" "github.com/drone/go-login/login/logger" "github.com/drone/go-login/login/stash" @@ -61,6 +62,13 @@ func main() { RedirectURL: *redirectURL, Scope: []string{"read_user", "api"}, } + case "gitee": + middleware = &gitee.Config{ + ClientID: *clientID, + ClientSecret: *clientSecret, + RedirectURL: *redirectURL, + Scope: []string{"user_info", "projects", "pull_requests", "hook"}, + } case "github": middleware = &github.Config{ ClientID: *clientID, @@ -162,4 +170,4 @@ func usage() { --redirect-url oauth redirect url --address http server address (:8080) --help display this help and exit`) -} +} \ No newline at end of file diff --git a/login/gitee/gitee.go b/login/gitee/gitee.go new file mode 100644 index 0000000..5252b88 --- /dev/null +++ b/login/gitee/gitee.go @@ -0,0 +1,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, "/") +} \ No newline at end of file