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

Allow the client to accept an oauth2.TokenSource #212

Open
ghstahl opened this issue May 21, 2023 · 3 comments
Open

Allow the client to accept an oauth2.TokenSource #212

ghstahl opened this issue May 21, 2023 · 3 comments

Comments

@ghstahl
Copy link

ghstahl commented May 21, 2023

I would like to use the OAuth2 clientcredentials tokensource when using the SDK.

Its working, but it requires that I coerce JWTProfileTokenSource into doing something that it wasn't meant to do.

The library should only take a oauth2.TokenSource, where JWTProfileTokenSource return an oauth2.TokenSouce.

The same would work for a PAT version (i.e. static token).

import (
 

	admin "github.com/zitadel/zitadel-go/v2/pkg/client/admin"

	middleware "github.com/zitadel/zitadel-go/v2/pkg/client/middleware"
	zitadel "github.com/zitadel/zitadel-go/v2/pkg/client/zitadel"

	"golang.org/x/oauth2/clientcredentials"
)
var clientcredentialsConfig *clientcredentials.Config = &clientcredentials.Config{
	ClientID:     saSystemReaderClientId,
	ClientSecret: saSystemReaderClientSecret,
	TokenURL:     "http://localhost:8081/oauth/v2/token",
	Scopes:       []string{oidc.ScopeOpenID, zitadel.ScopeZitadelAPI()},
}
func JWTProfileFromClientCredentials(cc *clientcredentials.Config) middleware.JWTProfileTokenSource {
	return func(issuer string, scopes []string) (oauth2.TokenSource, error) {
		return cc.TokenSource(context.Background()), nil
	}
}

....
adminReaderClient, err := admin.NewClient(
  *issuer,
  *api,
  []string{},
  zitadel.WithJWTProfileTokenSource(JWTProfileFromClientCredentials(clientcredentialsConfig)),
  zitadel.WithInsecure(),
  )
@ghstahl
Copy link
Author

ghstahl commented Aug 26, 2023

Personal Access Token example

// PATTokenSource ...
type PATTokenSource struct {
	PAT string
}

// Duration100Years ...
const Duration100Years = 100 * 365 * 24 * time.Hour

// Token ...
func (s *PATTokenSource) Token() (*oauth2.Token, error) {
	return &oauth2.Token{
		AccessToken: s.PAT,
		TokenType:   "Bearer",
		Expiry:      time.Now().Add(Duration100Years),
	}, nil
}

// PATJWTProfileTokenSource ...
func PATJWTProfileTokenSource(pat string) middleware.JWTProfileTokenSource {
	return func(issuer string, scopes []string) (oauth2.TokenSource, error) {
		return &PATTokenSource{
			PAT: pat,
		}, nil
	}
}

var options []zitadel.Option
options  = append(options , zitadel.WithJWTProfileTokenSource(startup.PATJWTProfileTokenSource(ZitadelPersonalAccessToken)))
options = append(options, zitadel.WithInsecure())

@fforootd
Copy link
Member

Thanks for sharing this.

We will work on our go sdk in the coming sprint and I think this will be a super input.

CC @hifabienne since I am not sure who will work on this I am going to tag you 😁

@hifabienne
Copy link
Member

I just made a reference in the issue for the go sdk/examples

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants