-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sdk: add wrapper library for HTTP Managers (#130)
Signed-off-by: Mohamedh Fazal <mohamedhfazal@gmail.com>
- Loading branch information
fazal
authored and
Aeneas
committed
Jul 3, 2016
1 parent
24d34b3
commit 266b324
Showing
4 changed files
with
427 additions
and
0 deletions.
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 | ||
skipTLSVerify 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.skipTLSVerify { | ||
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 { | ||
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 | ||
} | ||
} | ||
|
||
// SkipTLSVerify skips TLS verification | ||
func SkipTLSVerify() option { | ||
return func(c *Client) error { | ||
c.skipTLSVerify = 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.