This repository has been archived by the owner on May 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from hiendv/add-oauth
Add OAuth driver
- Loading branch information
Showing
169 changed files
with
32,524 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package internal | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
// HTTPClient is the interface for the common HTTP client | ||
type HTTPClient interface { | ||
Get(url string) (resp *http.Response, err error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package fixtures | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
"github.com/hiendv/gate" | ||
"github.com/hiendv/gate/internal" | ||
"github.com/pkg/errors" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
type contextKey string | ||
|
||
const noResponseKey contextKey = "no-response" | ||
const malformedResponseKey contextKey = "malformed-response" | ||
|
||
// OAuthClient is the mocking HTTP client for OAuth driver | ||
type OAuthClient struct { | ||
ctx context.Context | ||
token *oauth2.Token | ||
responses map[string]gate.HasEmail | ||
} | ||
|
||
// Get makes a GET request with the given URL | ||
func (client OAuthClient) Get(url string) (resp *http.Response, err error) { | ||
if noResponse, ok := client.ctx.Value(noResponseKey).(bool); ok && noResponse { | ||
return nil, nil | ||
} | ||
|
||
if client.token == nil || client.token.AccessToken == "" { | ||
err = errors.New("invalid token") | ||
return | ||
} | ||
|
||
if malformedResponse, ok := client.ctx.Value(malformedResponseKey).(bool); ok && malformedResponse { | ||
return &http.Response{ | ||
Body: ioutil.NopCloser(bytes.NewBufferString("malformed")), | ||
}, nil | ||
} | ||
|
||
user := client.responses[client.token.AccessToken] | ||
|
||
result, err := json.Marshal(user) | ||
if err != nil { | ||
return | ||
} | ||
|
||
return &http.Response{ | ||
Body: ioutil.NopCloser(bytes.NewBuffer(result)), | ||
}, nil | ||
} | ||
|
||
// OAuthProvider is the mocking provider for OAuth driver | ||
type OAuthProvider struct { | ||
Responses map[string]gate.HasEmail | ||
} | ||
|
||
// AuthCodeURL returns a URL to OAuth 2.0 provider's consent page | ||
func (config OAuthProvider) AuthCodeURL(state string, opts ...oauth2.AuthCodeOption) string { | ||
return "" | ||
} | ||
|
||
// Exchange converts an authorization code into a token | ||
func (config OAuthProvider) Exchange(ctx context.Context, code string) (*oauth2.Token, error) { | ||
if code == "" { | ||
return nil, nil | ||
} | ||
|
||
token := &oauth2.Token{} | ||
token.AccessToken = fmt.Sprintf("%s-token", code) | ||
|
||
return token, nil | ||
} | ||
|
||
// Client returns an HTTP client using the provided token | ||
func (config OAuthProvider) Client(ctx context.Context, token *oauth2.Token) internal.HTTPClient { | ||
return OAuthClient{ctx, token, config.Responses} | ||
} | ||
|
||
// BadOAuthProvider is the mocking provider with no client for OAuth driver | ||
type BadOAuthProvider struct { | ||
NoClient bool | ||
NoResponse bool | ||
MalformedResponse bool | ||
OAuthProvider | ||
} | ||
|
||
// Client returns an HTTP client using the provided token | ||
func (config BadOAuthProvider) Client(ctx context.Context, token *oauth2.Token) internal.HTTPClient { | ||
if config.NoClient { | ||
return nil | ||
} | ||
|
||
if config.NoResponse { | ||
ctx = context.WithValue(ctx, noResponseKey, true) | ||
} | ||
|
||
if config.MalformedResponse { | ||
ctx = context.WithValue(ctx, malformedResponseKey, true) | ||
} | ||
|
||
return config.OAuthProvider.Client(ctx, token) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package oauth | ||
|
||
import ( | ||
"github.com/hiendv/gate" | ||
"golang.org/x/oauth2" | ||
"golang.org/x/oauth2/facebook" | ||
"golang.org/x/oauth2/google" | ||
) | ||
|
||
// Config is the configuration for OAuth authentication | ||
type Config struct { | ||
gate.Config | ||
ClientID string | ||
ClientSecret string | ||
Scopes []string | ||
Endpoint oauth2.Endpoint | ||
RedirectURI string | ||
UserAPI string | ||
} | ||
|
||
// NewGoogleConfig is the constructor for OAuth configuration using Google API | ||
func NewGoogleConfig(base gate.Config, id, secret, redirectURI string) Config { | ||
return Config{ | ||
base, | ||
id, | ||
secret, | ||
[]string{"https://www.googleapis.com/auth/userinfo.email"}, | ||
google.Endpoint, | ||
redirectURI, | ||
"https://www.googleapis.com/oauth2/v3/userinfo", | ||
} | ||
} | ||
|
||
// NewFacebookConfig is the constructor for OAuth configuration using Facebook API | ||
func NewFacebookConfig(base gate.Config, id, secret, redirectURI string) Config { | ||
return Config{ | ||
base, | ||
id, | ||
secret, | ||
[]string{"email"}, | ||
facebook.Endpoint, | ||
redirectURI, | ||
"https://graph.facebook.com/v2.11/me?fields=id,name,email", | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package oauth is the OAuth2 authentication driver for github.com/hiendv/gate. It uses client implementations, not OAuth servers e.g. Google, Facebook, etc. | ||
package oauth |
Oops, something went wrong.