refactor interface

pull/1/head
Brad Rydzewski 6 years ago
parent 501e84cf0f
commit 865053da05

@ -40,28 +40,28 @@ func main() {
os.Exit(0)
}
var auther login.Authorizer
var middleware login.Middleware
switch *provider {
case "gogs", "gitea":
auther = &gogs.Authorizer{
middleware = &gogs.Config{
Login: "/login/form",
Server: *providerURL,
}
case "gitlab":
auther = &gitlab.Authorizer{
middleware = &gitlab.Config{
ClientID: *clientID,
ClientSecret: *clientSecret,
RedirectURL: *redirectURL,
Scope: []string{"read_user", "api"},
}
case "github":
auther = &github.Authorizer{
middleware = &github.Config{
ClientID: *clientID,
ClientSecret: *clientSecret,
Scope: []string{"repo", "user", "read:org"},
}
case "bitbucket":
auther = &bitbucket.Authorizer{
middleware = &bitbucket.Config{
ClientID: *clientID,
ClientSecret: *clientSecret,
RedirectURL: *redirectURL,
@ -71,7 +71,7 @@ func main() {
if err != nil {
log.Fatalln("Cannot parse Private Key. %s", err)
}
auther = &stash.Authorizer{
middleware = &stash.Config{
Address: *providerURL,
CallbackURL: *redirectURL,
ConsumerKey: *consumerKey,
@ -82,7 +82,7 @@ func main() {
// handles the authorization flow and displays the
// authorization results at completion.
http.Handle("/login/form", http.HandlerFunc(form))
http.Handle("/login", auther.Authorize(
http.Handle("/login", middleware.Handler(
http.HandlerFunc(details),
))

@ -7,32 +7,35 @@ package bitbucket
import (
"net/http"
"github.com/drone/go-login/login"
"github.com/drone/go-login/login/internal/oauth2"
)
var _ login.Middleware = (*Config)(nil)
const (
accessTokenURL = "https://bitbucket.org/site/oauth2/access_token"
authorizationURL = "https://bitbucket.org/site/oauth2/authorize"
)
// Authorizer configures a Bitbucket auth provider.
type Authorizer struct {
// Config configures a Bitbucket auth provider.
type Config struct {
Client *http.Client
ClientID string
ClientSecret string
RedirectURL string
}
// Authorize returns a http.Handler that runs h at the
// Handler returns a http.Handler that runs h at the
// completion of the GitHub authorization flow. The GitHub
// authorization details are available to h in the
// http.Request context.
func (a *Authorizer) Authorize(h http.Handler) http.Handler {
func (c *Config) Handler(h http.Handler) http.Handler {
return oauth2.Handler(h, &oauth2.Config{
Client: a.Client,
ClientID: a.ClientID,
ClientSecret: a.ClientSecret,
RedirectURL: a.RedirectURL,
Client: c.Client,
ClientID: c.ClientID,
ClientSecret: c.ClientSecret,
RedirectURL: c.RedirectURL,
AccessTokenURL: accessTokenURL,
AuthorizationURL: authorizationURL,
})

@ -8,12 +8,14 @@ import (
"net/http"
"strings"
"github.com/drone/go-login/login"
"github.com/drone/go-login/login/internal/oauth2"
)
// Authorizer configures a GitHub authorization
// provider.
type Authorizer struct {
var _ login.Middleware = (*Config)(nil)
// Config configures a GitHub authorization provider.
type Config struct {
Client *http.Client
ClientID string
ClientSecret string
@ -21,20 +23,20 @@ type Authorizer struct {
Scope []string
}
// Authorize returns a http.Handler that runs h at the
// Handler returns a http.Handler that runs h at the
// completion of the GitHub authorization flow. The GitHub
// authorization details are available to h in the
// http.Request context.
func (a *Authorizer) Authorize(h http.Handler) http.Handler {
server := normalizeAddress(a.Server)
func (c *Config) Handler(h http.Handler) http.Handler {
server := normalizeAddress(c.Server)
return oauth2.Handler(h, &oauth2.Config{
BasicAuthOff: true,
Client: a.Client,
ClientID: a.ClientID,
ClientSecret: a.ClientSecret,
Client: c.Client,
ClientID: c.ClientID,
ClientSecret: c.ClientSecret,
AccessTokenURL: server + "/login/oauth/access_token",
AuthorizationURL: server + "/login/oauth/authorize",
Scope: a.Scope,
Scope: c.Scope,
})
}

@ -8,11 +8,14 @@ import (
"net/http"
"strings"
"github.com/drone/go-login/login"
"github.com/drone/go-login/login/internal/oauth2"
)
// Authorizer configures the GitLab auth provider.
type Authorizer struct {
var _ login.Middleware = (*Config)(nil)
// Config configures the GitLab auth provider.
type Config struct {
ClientID string
ClientSecret string
RedirectURL string
@ -21,21 +24,21 @@ type Authorizer struct {
Client *http.Client
}
// Authorize returns a http.Handler that runs h at the
// Handler returns a http.Handler that runs h at the
// completion of the GitLab authorization flow. The GitLab
// authorization details are available to h in the
// http.Request context.
func (a *Authorizer) Authorize(h http.Handler) http.Handler {
server := normalizeAddress(a.Server)
func (c *Config) Handler(h http.Handler) http.Handler {
server := normalizeAddress(c.Server)
return oauth2.Handler(h, &oauth2.Config{
BasicAuthOff: true,
Client: a.Client,
ClientID: a.ClientID,
ClientSecret: a.ClientSecret,
RedirectURL: a.RedirectURL,
Client: c.Client,
ClientID: c.ClientID,
ClientSecret: c.ClientSecret,
RedirectURL: c.RedirectURL,
AccessTokenURL: server + "/oauth/token",
AuthorizationURL: server + "/oauth/authorize",
Scope: a.Scope,
Scope: c.Scope,
})
}

@ -7,27 +7,31 @@ package gogs
import (
"net/http"
"strings"
"github.com/drone/go-login/login"
)
// Authorizer configures the Gogs auth provider.
type Authorizer struct {
var _ login.Middleware = (*Config)(nil)
// Config configures the Gogs auth provider.
type Config struct {
Label string
Login string
Server string
Client *http.Client
}
// Authorize returns a http.Handler that runs h at the
// Handler returns a http.Handler that runs h at the
// completion of the GitLab authorization flow. The GitLab
// authorization details are available to h in the
// http.Request context.
func (a *Authorizer) Authorize(h http.Handler) http.Handler {
func (c *Config) Handler(h http.Handler) http.Handler {
v := &handler{
next: h,
label: a.Label,
login: a.Login,
server: strings.TrimSuffix(a.Server, "/"),
client: a.Client,
label: c.Label,
login: c.Login,
server: strings.TrimSuffix(c.Server, "/"),
client: c.Client,
}
if v.client == nil {
v.client = http.DefaultClient

@ -10,21 +10,12 @@ import (
"time"
)
// Authorizer returns a http.Handler that runs h at the
// completion of the authorization flow. The authorization
// results are available to h in the http.Request context.
type Authorizer interface {
Authorize(h http.Handler) http.Handler
}
// Middleware defines a Login middleware. The middleware
// wraps the http.Handler and intercepts the http.Request
// to perform authentication. The http.Handler is invoked
// when authentication is complete, with authentication
// details (oauth token, etc) passed to the handler via
// the http.Request context.
// Middleware provides login middleware.
type Middleware interface {
Wrap(h http.Handler) http.Handler
// Handler returns a http.Handler that runs h at the
// completion of the authorization flow. The authorization
// results are available to h in the http.Request context.
Handler(h http.Handler) http.Handler
}
// Token represents an authorization token.

@ -13,18 +13,21 @@ import (
"net/http"
"strings"
"github.com/drone/go-login/login"
"github.com/drone/go-login/login/internal/oauth1"
)
var _ login.Middleware = (*Config)(nil)
const (
requestTokenURL = "%s/plugins/servlet/oauth/request-token"
authorizeTokenURL = "%s/plugins/servlet/oauth/authorize"
accessTokenURL = "%s/plugins/servlet/oauth/access-token"
)
// Authorizer configures the Bitbucket Server (Stash)
// authorization provider.
type Authorizer struct {
// Config configures the Bitbucket Server (Stash)
// authorization middleware.
type Config struct {
Address string
ConsumerKey string
ConsumerSecret string
@ -33,21 +36,21 @@ type Authorizer struct {
Client *http.Client
}
// Authorize returns a http.Handler that runs h at the
// Handler returns a http.Handler that runs h at the
// completion of the GitHub authorization flow. The GitHub
// authorization details are available to h in the
// http.Request context.
func (a *Authorizer) Authorize(h http.Handler) http.Handler {
server := strings.TrimSuffix(a.Address, "/")
func (c *Config) Handler(h http.Handler) http.Handler {
server := strings.TrimSuffix(c.Address, "/")
signer := &oauth1.RSASigner{
PrivateKey: a.PrivateKey,
PrivateKey: c.PrivateKey,
}
return oauth1.Handler(h, &oauth1.Config{
Signer: signer,
Client: a.Client,
ConsumerKey: a.ConsumerKey,
ConsumerSecret: a.ConsumerSecret,
CallbackURL: a.CallbackURL,
Client: c.Client,
ConsumerKey: c.ConsumerKey,
ConsumerSecret: c.ConsumerSecret,
CallbackURL: c.CallbackURL,
AccessTokenURL: fmt.Sprintf(accessTokenURL, server),
AuthorizationURL: fmt.Sprintf(authorizeTokenURL, server),
RequestTokenURL: fmt.Sprintf(requestTokenURL, server),

Loading…
Cancel
Save