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 ¶
Create an organization, make a link to its IdP Wizard, and send an audit event. This example is compiled but not run by `go test`.
package main
import (
"context"
"fmt"
"log"
"os"
"time"
"github.com/p2-inc/phasetwo-go"
"github.com/p2-inc/phasetwo-go/openapi"
)
func main() {
ctx := context.Background()
realm := "my-realm"
client, err := phasetwo.New(ctx, phasetwo.Config{
BaseURL: "https://auth.example.com/auth",
TokenRealm: realm,
ClientID: "api-client",
ClientSecret: os.Getenv("PHASETWO_CLIENT_SECRET"),
})
if err != nil {
log.Fatal(err)
}
// 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 {
log.Fatal(err)
}
if err := phasetwo.Check(created.HTTPResponse, created.Body); err != nil {
log.Fatal(err)
}
orgID, _ := phasetwo.IDFromLocation(created.HTTPResponse)
// Create a link to the IdP Wizard for the organization
link, err := client.CreatePortalLinkWithFormdataBodyWithResponse(ctx, realm, orgID, openapi.CreatePortalLinkFormdataRequestBody{})
if err != nil {
log.Fatal(err)
}
fmt.Println(*link.JSON200.Link)
// Send an audit event
eventType := "foo.bar"
now := time.Now().UnixMilli()
if _, err := client.CreateEventWithResponse(ctx, realm, openapi.EventRepresentation{Type: &eventType, Time: &now}); err != nil {
log.Fatal(err)
}
}
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.