Skip to content

Commit

Permalink
feat: implement Oauth2Credential service
Browse files Browse the repository at this point in the history
  • Loading branch information
hbagdi committed Aug 21, 2019
1 parent 7e75d4b commit 1cf248e
Show file tree
Hide file tree
Showing 6 changed files with 545 additions and 0 deletions.
16 changes: 16 additions & 0 deletions kong/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ func (c HMACAuth) id() *string {
return c.ID
}

// Oauth2Credential represents a Oauth2 credential in Kong.
// +k8s:deepcopy-gen=true
type Oauth2Credential struct {
Consumer *Consumer `json:"consumer,omitempty" yaml:"consumer,omitempty"`
CreatedAt *int `json:"created_at,omitempty" yaml:"created_at,omitempty"`
ID *string `json:"id,omitempty" yaml:"id,omitempty"`
Name *string `json:"name,omitempty" yaml:"name,omitempty"`
ClientID *string `json:"client_id,omitempty" yaml:"client_id,omitempty"`
ClientSecret *string `json:"client_secret,omitempty" yaml:"client_secret,omitempty"`
RedirectURIs []*string `json:"redirect_uris,omitempty" yaml:"redirect_uris,omitempty"`
}

func (c Oauth2Credential) id() *string {
return c.ID
}

// JWTAuth represents a JWT credential in Kong.
// +k8s:deepcopy-gen=true
type JWTAuth struct {
Expand Down
1 change: 1 addition & 0 deletions kong/credentials_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var (
"hmac-auth": "hmac-auth",
"jwt-auth": "jwt",
"acl": "acls",
"oauth2": "oauth2",
}
)

Expand Down
4 changes: 4 additions & 0 deletions kong/kong.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type Client struct {
JWTAuths *JWTAuthService
ACLs *ACLService

Oauth2Credentials *Oauth2Service

logger io.Writer
debug bool
CustomEntities *CustomEntityService
Expand Down Expand Up @@ -101,6 +103,8 @@ func NewClient(baseURL *string, client *http.Client) (*Client, error) {
kong.JWTAuths = (*JWTAuthService)(&kong.common)
kong.ACLs = (*ACLService)(&kong.common)

kong.Oauth2Credentials = (*Oauth2Service)(&kong.common)

kong.CustomEntities = (*CustomEntityService)(&kong.common)
kong.Registry = custom.NewDefaultRegistry()

Expand Down
150 changes: 150 additions & 0 deletions kong/oauth2_auth_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package kong

import (
"context"
"encoding/json"
)

// Oauth2Service handles oauth2 credentials in Kong.
type Oauth2Service service

// Create creates an oauth2 credential in Kong
// If an ID is specified, it will be used to
// create a oauth2 credential in Kong, otherwise an ID
// is auto-generated.
func (s *Oauth2Service) Create(ctx context.Context,
consumerUsernameOrID *string,
oauth2Cred *Oauth2Credential) (*Oauth2Credential, error) {

cred, err := s.client.credentials.Create(ctx, "oauth2",
consumerUsernameOrID, oauth2Cred)
if err != nil {
return nil, err
}

var createdOauth2Cred Oauth2Credential
err = json.Unmarshal(cred, &createdOauth2Cred)
if err != nil {
return nil, err
}

return &createdOauth2Cred, nil
}

// Get fetches an oauth2 credential from Kong.
func (s *Oauth2Service) Get(ctx context.Context,
consumerUsernameOrID, clientIDorID *string) (*Oauth2Credential, error) {

cred, err := s.client.credentials.Get(ctx, "oauth2",
consumerUsernameOrID, clientIDorID)
if err != nil {
return nil, err
}

var oauth2Cred Oauth2Credential
err = json.Unmarshal(cred, &oauth2Cred)
if err != nil {
return nil, err
}

return &oauth2Cred, nil
}

// Update updates an oauth2 credential in Kong.
func (s *Oauth2Service) Update(ctx context.Context,
consumerUsernameOrID *string,
oauth2Cred *Oauth2Credential) (*Oauth2Credential, error) {

cred, err := s.client.credentials.Update(ctx, "oauth2",
consumerUsernameOrID, oauth2Cred)
if err != nil {
return nil, err
}

var updatedHMACAuth Oauth2Credential
err = json.Unmarshal(cred, &updatedHMACAuth)
if err != nil {
return nil, err
}

return &updatedHMACAuth, nil
}

// Delete deletes an oauth2 credential in Kong.
func (s *Oauth2Service) Delete(ctx context.Context,
consumerUsernameOrID, clientIDorID *string) error {
return s.client.credentials.Delete(ctx, "oauth2",
consumerUsernameOrID, clientIDorID)
}

// List fetches a list of oauth2 credentials in Kong.
// opt can be used to control pagination.
func (s *Oauth2Service) List(ctx context.Context,
opt *ListOpt) ([]*Oauth2Credential, *ListOpt, error) {
data, next, err := s.client.list(ctx, "/oauth2", opt)
if err != nil {
return nil, nil, err
}
var oauth2Creds []*Oauth2Credential
for _, object := range data {
b, err := object.MarshalJSON()
if err != nil {
return nil, nil, err
}
var oauth2Cred Oauth2Credential
err = json.Unmarshal(b, &oauth2Cred)
if err != nil {
return nil, nil, err
}
oauth2Creds = append(oauth2Creds, &oauth2Cred)
}

return oauth2Creds, next, nil
}

// ListAll fetches all oauth2 credentials in Kong.
// This method can take a while if there
// a lot of oauth2 credentials present.
func (s *Oauth2Service) ListAll(
ctx context.Context) ([]*Oauth2Credential, error) {
var oauth2Creds, data []*Oauth2Credential
var err error
opt := &ListOpt{Size: pageSize}

for opt != nil {
data, opt, err = s.List(ctx, opt)
if err != nil {
return nil, err
}
oauth2Creds = append(oauth2Creds, data...)
}
return oauth2Creds, nil
}

// ListForConsumer fetches a list of oauth2 credentials
// in Kong associated with a specific consumer.
// opt can be used to control pagination.
func (s *Oauth2Service) ListForConsumer(ctx context.Context,
consumerUsernameOrID *string, opt *ListOpt) ([]*Oauth2Credential,
*ListOpt, error) {
data, next, err := s.client.list(ctx,
"/consumers/"+*consumerUsernameOrID+"/oauth2", opt)
if err != nil {
return nil, nil, err
}
var oauth2Creds []*Oauth2Credential
for _, object := range data {
b, err := object.MarshalJSON()
if err != nil {
return nil, nil, err
}
var oauth2Cred Oauth2Credential
err = json.Unmarshal(b, &oauth2Cred)
if err != nil {
return nil, nil, err
}
oauth2Creds = append(oauth2Creds, &oauth2Cred)
}

return oauth2Creds, next, nil
}
Loading

0 comments on commit 1cf248e

Please sign in to comment.