-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add wrapper library for HTTP Managers #130
Merged
Merged
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
349628c
mount warden handler
mfzl 6536c9a
Merge branch 'master' of https://github.com/ory-am/hydra
mfzl c614374
Merge branch 'master' of https://github.com/ory-am/hydra
mfzl 3a392cd
added a thin wrapper for HTTP Manager's to be used as a HTTP client l…
mfzl 0eb36d4
Merge branch 'master' of https://github.com/ory-am/hydra
mfzl 3ae3679
- renamed package to sdk
mfzl 579ba3f
readme: add a small description about the HTTP Managers and SDK
mfzl b98faea
readme: markdown fix
78cbe90
readme: typo
mfzl 0456935
sdk: document exported options
mfzl c41c5fd
Merge branch 'master' of https://github.com/faxal/hydra
mfzl 4a09400
readme: add quick introduction to SDK
mfzl 1753fac
readme: fix numbering
mfzl 396d75a
readme: fix numbering
mfzl c72109b
Merge branch 'master' of https://github.com/faxal/hydra
mfzl b96253c
sdk: added tests for exported options
mfzl a9d6398
sdk: rename SDK option SkipSSL to SkipTLSVerify
mfzl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,129 @@ | ||
// Wraps hydra HTTP Manager's | ||
package sdk | ||
|
||
import ( | ||
"crypto/tls" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
|
||
"github.com/ory-am/hydra/client" | ||
"github.com/ory-am/hydra/connection" | ||
"github.com/ory-am/hydra/jwk" | ||
"github.com/ory-am/hydra/pkg" | ||
"github.com/ory-am/hydra/policy" | ||
"github.com/ory-am/hydra/warden" | ||
|
||
"golang.org/x/net/context" | ||
"golang.org/x/oauth2" | ||
"golang.org/x/oauth2/clientcredentials" | ||
) | ||
|
||
type Client struct { | ||
http *http.Client | ||
clusterURL *url.URL | ||
clientID string | ||
clientSecret string | ||
skipSSL bool | ||
scopes []string | ||
|
||
credentials clientcredentials.Config | ||
|
||
Client *client.HTTPManager | ||
SSO *connection.HTTPManager | ||
JWK *jwk.HTTPManager | ||
Policies *policy.HTTPManager | ||
Warden *warden.HTTPWarden | ||
} | ||
|
||
type option func(*Client) error | ||
|
||
// default options for hydra client | ||
var defaultOptions = []option{ | ||
ClusterURL(os.Getenv("HYDRA_CLUSTER_URL")), | ||
ClientID(os.Getenv("HYDRA_CLIENT_ID")), | ||
ClientSecret(os.Getenv("HYDRA_CLIENT_SECRET")), | ||
Scopes("core", "hydra"), | ||
} | ||
|
||
// Connect instantiates a new client to communicate with Hydra | ||
func Connect(opts ...option) (*Client, error) { | ||
c := &Client{} | ||
|
||
var err error | ||
// apply default options | ||
for _, opt := range defaultOptions { | ||
err = opt(c) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
// override any default values with given options | ||
for _, opt := range opts { | ||
err = opt(c) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
c.credentials = clientcredentials.Config{ | ||
ClientID: c.clientID, | ||
ClientSecret: c.clientSecret, | ||
TokenURL: pkg.JoinURL(c.clusterURL, "oauth2/token").String(), | ||
Scopes: c.scopes, | ||
} | ||
|
||
c.http = http.DefaultClient | ||
|
||
if c.skipSSL { | ||
c.http = &http.Client{ | ||
Transport: &http.Transport{ | ||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, | ||
}, | ||
} | ||
} | ||
|
||
err = c.authenticate() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// initialize service endpoints | ||
c.Client = &client.HTTPManager{ | ||
Endpoint: pkg.JoinURL(c.clusterURL, "/clients"), | ||
Client: c.http, | ||
} | ||
|
||
c.SSO = &connection.HTTPManager{ | ||
Endpoint: pkg.JoinURL(c.clusterURL, "/connections"), | ||
Client: c.http, | ||
} | ||
|
||
c.JWK = &jwk.HTTPManager{ | ||
Endpoint: pkg.JoinURL(c.clusterURL, "/keys"), | ||
Client: c.http, | ||
} | ||
|
||
c.Policies = &policy.HTTPManager{ | ||
Endpoint: pkg.JoinURL(c.clusterURL, "/policies"), | ||
Client: c.http, | ||
} | ||
|
||
c.Warden = &warden.HTTPWarden{ | ||
Client: c.http, | ||
} | ||
|
||
return c, nil | ||
} | ||
|
||
func (h *Client) authenticate() error { | ||
ctx := context.WithValue(oauth2.NoContext, oauth2.HTTPClient, h.http) | ||
_, err := h.credentials.Token(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
h.http = h.credentials.Client(ctx) | ||
return nil | ||
} |
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,83 @@ | ||
package sdk | ||
|
||
import ( | ||
"io/ioutil" | ||
"net/url" | ||
|
||
"gopkg.in/yaml.v1" | ||
) | ||
|
||
// ClusterURL sets Hydra service URL | ||
func ClusterURL(urlStr string) option { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please document exported functions :) |
||
return func(c *Client) error { | ||
var err error | ||
c.clusterURL, err = url.Parse(urlStr) | ||
return err | ||
} | ||
} | ||
|
||
type hydraConfig struct { | ||
ClusterURL string `yaml:"cluster_url"` | ||
ClientID string `yaml:"client_id"` | ||
ClientSecret string `yaml:"client_secret"` | ||
} | ||
|
||
// FromYAML loads configurations from a YAML file | ||
func FromYAML(file string) option { | ||
return func(c *Client) error { | ||
var err error | ||
var config = hydraConfig{} | ||
|
||
data, err := ioutil.ReadFile(file) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = yaml.Unmarshal(data, &config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
c.clusterURL, err = url.Parse(config.ClusterURL) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
c.clientID = config.ClientID | ||
c.clientSecret = config.ClientSecret | ||
|
||
return nil | ||
} | ||
} | ||
|
||
// ClientID sets OAuth client ID | ||
func ClientID(id string) option { | ||
return func(c *Client) error { | ||
c.clientID = id | ||
return nil | ||
} | ||
} | ||
|
||
// ClientSecret sets OAuth client secret | ||
func ClientSecret(secret string) option { | ||
return func(c *Client) error { | ||
c.clientSecret = secret | ||
return nil | ||
} | ||
} | ||
|
||
// SkipSSL skips TLS verification | ||
func SkipSSL() option { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you rename this to |
||
return func(c *Client) error { | ||
c.skipSSL = true | ||
return nil | ||
} | ||
} | ||
|
||
// Scopes sets client scopes granted by Hydra | ||
func Scopes(scopes ...string) option { | ||
return func(c *Client) error { | ||
c.scopes = scopes | ||
return nil | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add examples here? e.g.