Documentation
¶
Overview ¶
Package phasetwo is a Go client for the Phase Two Admin REST API: the organizations, SSO, webhooks, events and other extensions that Phase Two adds to Keycloak.
The API operations and models are generated into the openapi package from the Phase Two OpenAPI spec. This package adds the hand-written parts: building an authenticated client (New), getting the id of a created resource (IDFromLocation) and turning error responses into errors (Check).
For core Keycloak administration (realms, users, clients), use a Keycloak admin client alongside this one.
Example ¶
Connect with a service account, then run the quick start. This example is compiled but not run by `go test`; the integration test runs quickstart against Phase Two Keycloak.
package main
import (
"context"
"fmt"
"log"
"os"
"time"
"github.com/p2-inc/phasetwo-go"
"github.com/p2-inc/phasetwo-go/openapi"
)
// Connect with a service account, then run the quick start. This example is
// compiled but not run by `go test`; the integration test runs quickstart
// against Phase Two Keycloak.
func main() {
// snippet:start client
ctx := context.Background()
realm := "my-realm"
client, err := phasetwo.New(ctx, phasetwo.Config{
BaseURL: "https://auth.example.com/auth", // your Keycloak base URL
TokenRealm: realm, // where the service account lives
ClientID: "api-client",
ClientSecret: os.Getenv("PHASETWO_CLIENT_SECRET"),
})
if err != nil {
log.Fatal(err)
}
// snippet:end client
if _, err := quickstart(ctx, client, realm, "a-user-id"); err != nil {
log.Fatal(err)
}
}
// quickstart creates an organization, adds a member, makes an IdP Wizard link
// for the organization and sends an audit event. It returns the organization id.
func quickstart(ctx context.Context, client *phasetwo.Client, realm, userID string) (string, error) {
// snippet:start quickstart
// Create an organization. The new id is in the Location header.
name := "example"
created, err := client.CreateOrganizationWithResponse(ctx, realm,
openapi.OrganizationRepresentation{Name: &name})
if err != nil {
return "", err
}
if err := phasetwo.Check(created.HTTPResponse, created.Body); err != nil {
return "", err
}
orgID, err := phasetwo.IDFromLocation(created.HTTPResponse)
if err != nil {
return "", err
}
// Add an existing user as a member.
added, err := client.AddOrganizationMemberWithResponse(ctx, realm, orgID, userID)
if err != nil {
return "", err
}
if err := phasetwo.Check(added.HTTPResponse, added.Body); err != nil {
return "", err
}
// Create a link to the IdP Wizard, so the organization's admin can set up SSO.
link, err := client.CreatePortalLinkWithFormdataBodyWithResponse(ctx, realm, orgID,
openapi.CreatePortalLinkFormdataRequestBody{})
if err != nil {
return "", err
}
if err := phasetwo.Check(link.HTTPResponse, link.Body); err != nil {
return "", err
}
fmt.Println("IdP Wizard:", *link.JSON200.Link)
// Send an audit event.
eventType := "example.created"
now := time.Now().UnixMilli()
sent, err := client.CreateEventWithResponse(ctx, realm,
openapi.EventRepresentation{Type: &eventType, Time: &now})
if err != nil {
return "", err
}
if err := phasetwo.Check(sent.HTTPResponse, sent.Body); err != nil {
return "", err
}
// snippet:end quickstart
return orgID, nil
}
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Check ¶
Check returns an *APIError if resp isn't a 2xx response. Pass the HTTPResponse and Body fields of any ...WithResponse result:
resp, err := client.GetOrganizationByIdWithResponse(ctx, realm, id)
if err != nil { ... }
if err := phasetwo.Check(resp.HTTPResponse, resp.Body); err != nil { ... }
func IDFromLocation ¶
IDFromLocation returns the id of a resource the API just created: the last path segment of the response's Location header. Create operations return 201 with an empty body, so this is how to get the new id:
resp, err := client.CreateOrganizationWithResponse(ctx, realm, org) ... id, err := phasetwo.IDFromLocation(resp.HTTPResponse)
Types ¶
type APIError ¶
type APIError struct {
StatusCode int
// Body is the raw response body; Keycloak usually sends {"error": "..."} JSON.
Body []byte
}
APIError is an error response from the Phase Two API.
type Client ¶
type Client struct {
*openapi.ClientWithResponses
}
Client is an authenticated Phase Two API client. All API operations are methods of the embedded openapi.ClientWithResponses; every operation takes the realm name as its first argument after the context.
type Config ¶
type Config struct {
// BaseURL is your Keycloak base URL, including the context path if it has one,
// e.g. "https://auth.example.com/auth". Required.
BaseURL string
// TokenRealm, ClientID and ClientSecret get tokens with the client credentials
// grant from TokenRealm's token endpoint, typically a service account in the
// realm you manage. Ignored when TokenSource is set.
TokenRealm string
ClientID string
ClientSecret string
// TokenSource supplies access tokens instead of the client credentials grant,
// e.g. a token from your own login flow.
TokenSource oauth2.TokenSource
// HTTPClient is used for both token and API requests (timeouts, proxies,
// custom transports). Defaults to http.DefaultClient.
HTTPClient *http.Client
// UserAgent is sent on API requests. Defaults to "phasetwo-go".
UserAgent string
}
Config configures a Client.