-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
3,992 additions
and
1 deletion.
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
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,50 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"log" | ||
|
||
"github.com/zitadel/zitadel-go/v2/pkg/client/system" | ||
pb "github.com/zitadel/zitadel-go/v2/pkg/client/zitadel/system" | ||
) | ||
|
||
var ( | ||
issuer = flag.String("issuer", "", "issuer of your ZITADEL instance (in the form: https://<instance>.zitadel.cloud or https://<yourdomain>)") | ||
api = flag.String("api", "", "gRPC endpoint of your ZITADEL instance (in the form: <instance>.zitadel.cloud:443 or <yourdomain>:443)") | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
//create a client for the system api providing: | ||
//- issuer (e.g. https://acme-dtfhdg.zitadel.cloud) | ||
//- api (e.g. acme-dtfhdg.zitadel.cloud:443) | ||
//- a JWT Profile source token (e.g. path to your key.pem) and the corresponding userID from the `SystemAPIUsers` config in ZITADEL | ||
client, err := system.NewClient( | ||
*issuer, | ||
*api, | ||
system.JWTProfileFromPath("system_user_1.pem", "system_user_1"), | ||
) | ||
if err != nil { | ||
log.Fatalln("could not create client", err) | ||
} | ||
defer func() { | ||
err := client.Connection.Close() | ||
if err != nil { | ||
log.Println("could not close grpc connection", err) | ||
} | ||
}() | ||
|
||
ctx := context.Background() | ||
//call ZITADEL and print the name and creation date of all instances | ||
//the call was successful if no error occurred | ||
resp, err := client.ListInstances(ctx, &pb.ListInstancesRequest{}) | ||
if err != nil { | ||
log.Fatalln("call failed: ", err) | ||
} | ||
//print all instances | ||
for _, instance := range resp.GetResult() { | ||
log.Printf("%s was created on %s", instance.Name, instance.Details.CreationDate.AsTime()) | ||
} | ||
} |
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,106 @@ | ||
package system | ||
|
||
import ( | ||
"io/ioutil" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/zitadel/oidc/pkg/client" | ||
"github.com/zitadel/oidc/pkg/oidc" | ||
"golang.org/x/oauth2" | ||
"gopkg.in/square/go-jose.v2" | ||
|
||
"github.com/zitadel/zitadel-go/v2/pkg/client/middleware" | ||
"github.com/zitadel/zitadel-go/v2/pkg/client/zitadel" | ||
"github.com/zitadel/zitadel-go/v2/pkg/client/zitadel/system" | ||
) | ||
|
||
type Client struct { | ||
Connection *zitadel.Connection | ||
system.SystemServiceClient | ||
} | ||
|
||
func NewClient(issuer, api string, source JWTAuthenticationSource, opts ...Option) (*Client, error) { | ||
options := make([]zitadel.Option, len(opts)+1) | ||
options[0] = zitadel.WithJWTProfileTokenSource(source()) | ||
for i, opt := range opts { | ||
options[i+1] = opt() | ||
} | ||
conn, err := zitadel.NewConnection(issuer, api, nil, options...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &Client{ | ||
Connection: conn, | ||
SystemServiceClient: system.NewSystemServiceClient(conn.ClientConn), | ||
}, nil | ||
} | ||
|
||
type Option func() zitadel.Option | ||
|
||
// WithInsecure disables transport security for the client connection | ||
// use only when absolutely necessary (e.g. local development) | ||
func WithInsecure() func() zitadel.Option { | ||
return func() zitadel.Option { | ||
return zitadel.WithInsecure() | ||
} | ||
} | ||
|
||
type JWTAuthenticationSource func() middleware.JWTProfileTokenSource | ||
|
||
// JWTProfileFromPath reads the key at the provided path and creates a JWTAuthenticationSource (oauth2.TokensSource) | ||
// to authenticate against the SystemAPI | ||
func JWTProfileFromPath(keyPath, userID string) JWTAuthenticationSource { | ||
return func() middleware.JWTProfileTokenSource { | ||
return func(issuer string, _ []string) (oauth2.TokenSource, error) { | ||
key, err := ioutil.ReadFile(keyPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return jwtAuthenticationTokenSource(issuer, userID, key) | ||
} | ||
} | ||
} | ||
|
||
// JWTProfileFromKey creates a JWTAuthenticationSource (oauth2.TokensSource) from the provided key (and userID) | ||
// to authenticate against the SystemAPI | ||
func JWTProfileFromKey(key []byte, userID string) JWTAuthenticationSource { | ||
return func() middleware.JWTProfileTokenSource { | ||
return func(issuer string, _ []string) (oauth2.TokenSource, error) { | ||
return jwtAuthenticationTokenSource(issuer, userID, key) | ||
} | ||
} | ||
} | ||
|
||
type jwtAuthentication struct { | ||
userID string | ||
audience []string | ||
signer jose.Signer | ||
httpClient *http.Client | ||
} | ||
|
||
func jwtAuthenticationTokenSource(issuer, userID string, key []byte) (oauth2.TokenSource, error) { | ||
signer, err := client.NewSignerFromPrivateKeyByte(key, "") | ||
if err != nil { | ||
return nil, err | ||
} | ||
source := &jwtAuthentication{ | ||
userID: userID, | ||
audience: []string{issuer}, | ||
signer: signer, | ||
httpClient: http.DefaultClient, | ||
} | ||
return source, nil | ||
} | ||
|
||
func (j *jwtAuthentication) Token() (*oauth2.Token, error) { | ||
token, err := client.SignedJWTProfileAssertion(j.userID, j.audience, time.Hour, j.signer) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &oauth2.Token{ | ||
AccessToken: token, | ||
TokenType: oidc.BearerToken, | ||
Expiry: time.Now().Add(time.Hour - 5*time.Second), | ||
}, nil | ||
} |
Oops, something went wrong.