Skip to content
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 token source implementation #39

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions kc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"context"
"fmt"
"runtime/debug"
"time"

"github.com/kollalabs/sdk-go/kc/swagger"
"golang.org/x/oauth2"
)

const baseURL = "https://api.getkolla.com/connect"
Expand Down Expand Up @@ -113,6 +115,45 @@ func (c *Client) Credentials(ctx context.Context, connectorID string, consumerID
return creds, nil
}

type tokenSource struct {
connectorID string
consumerID string

credentials *Credentials
kc *Client
}

func (t *tokenSource) Token() (*oauth2.Token, error) {
if t.kc == nil || t.connectorID == "" || t.consumerID == "" {
return nil, fmt.Errorf("token source not configured")
}

if t.credentials.ExpiryTime.Before(time.Now().Add(-time.Second * 10)) {
// fetch the credentials
creds, err := t.kc.Credentials(context.Background(), t.connectorID, t.consumerID)
if err != nil {
return nil, err
}
t.credentials = creds
}

return &oauth2.Token{
AccessToken: t.credentials.Token,
Expiry: t.credentials.ExpiryTime,
}, nil

}

func (c *Client) CredentialsOAuth2TokenSource(ctx context.Context, connectorID string, consumerID string) (oauth2.TokenSource, error) {

t := &tokenSource{
connectorID: connectorID,
consumerID: consumerID,
}

return t, nil
}

func setUserAgent() string {
userAgent := ""
buildInfo, ok := debug.ReadBuildInfo()
Expand Down