Configuration and User Management SDKs

pingfederate-go-client (Go)

The pingfederate-go-client SDK has not yet been released to version 1.0.0. While general use of the SDK is not expected to change and is stable, later versions could contain minor changes to package and function names.

Use the PingFederate Go SDK to connect your Go application to your PingFederate deployment.

The pingfederate-go-client module provides API bindings to the PingFederate configuration API, allowing developers to invoke API services simply using strongly typed request and response payloads. The module can be included as a dependency in developer code.

Prerequisites

The pingfederate-go-client module requires a running PingFederate admin node instance. The module requires the use of the PingFederate administrative API.

Learn more about the PingFederate administrative API and how to configure access in the PingFederate administrative API documentation.

Getting started

The following example shows how to include the pingfederate-go-client module in a developer project.

If you haven’t done so already, initialize a new Go module project:

go mod init github.com/mygithubuser/my-go-project

To determine the version of the pingfederate-go-client to use, take the full PingFederate server version and omit the version separator.

For example, for a client for PingFederate version 12.2.0, the version of the Go client to use will be v1220.

Use the standard Go commands to install pingfederate-go-client to the project for your version of PingFederate:

go get github.com/pingidentity/pingfederate-go-client/v1220

Connect to the service

The following sections describe the available options to connect the configuration management SDK to the PingFederate service.

OAuth 2.0 client credentials

When connecting to the PingFederate service, you can use the OAuth 2.0 client credentials grant as in the following example:

package main

import (
	"context"
	"crypto/tls"
	"fmt"
	"net/http"

	pingfederate "github.com/pingidentity/pingfederate-go-client/v1220/configurationapi"
)

func main() {

	pfHttpHost := "https://localhost:9999"
	pfAdminApiPath := "/pf-admin-api/v1"
	pfTokenUrl := "https://localhost:9031/as/token.oauth2"
	pfClientId := "my-client-id"
	pfClientSecret := "my-client-secret"
	pfScopes := []string{"test-scope1", "test-scope2"}

	// Initialize the API client
	clientConfig := pingfederate.NewConfiguration()
	clientConfig.DefaultHeader["X-Xsrf-Header"] = "PingFederate"
	clientConfig.DefaultHeader["X-BypassExternalValidation"] = "false"
	clientConfig.Servers = pingfederate.ServerConfigurations{
		{
			URL: pfHttpHost + pfAdminApiPath,
		},
	}
	tr := &http.Transport{
		TLSClientConfig: &tls.Config{
			InsecureSkipVerify: false,
		},
	}
	clientConfig.HTTPClient = &http.Client{Transport: tr}

	apiClient := pingfederate.NewAPIClient(clientConfig)

	// Set OAuth 2.0 credentials in the Go context
	oauth2AuthContext := context.WithValue(context.Background(), pingfederate.ContextOAuth2, pingfederate.OAuthValues{
		Transport:    tr,
		TokenUrl:     pfTokenUrl,
		ClientId:     pfClientId,
		ClientSecret: pfClientSecret,
		Scopes:       pfScopes,
	})

	// Call an API from the `apiClient` object
	readResponse, httpResponse, err := apiClient.IdpAdaptersAPI.GetIdpAdapters(oauth2AuthContext).Execute()
	if err != nil {
		panic(err)
	}

	fmt.Printf("Response HTTP Code: %d", httpResponse.StatusCode)
	fmt.Printf("Response: %v", readResponse)
}

HTTP basic authentication

When connecting to the PingFederate service, you can use HTTP basic authentication as in the following example:

package main

import (
	"context"
	"crypto/tls"
	"fmt"
	"net/http"

	pingfederate "github.com/pingidentity/pingfederate-go-client/v1220/configurationapi"
)

func main() {

	pfHttpHost := "https://localhost:9999"
	pfAdminApiPath := "/pf-admin-api/v1"
	pfUsername := "administrator"
	pfPassword := "my-admin-password"

	// Initialize the API client
	clientConfig := pingfederate.NewConfiguration()
	clientConfig.DefaultHeader["X-Xsrf-Header"] = "PingFederate"
	clientConfig.DefaultHeader["X-BypassExternalValidation"] = "false"
	clientConfig.Servers = pingfederate.ServerConfigurations{
		{
			URL: pfHttpHost + pfAdminApiPath,
		},
	}
	tr := &http.Transport{
		TLSClientConfig: &tls.Config{
			InsecureSkipVerify: false,
		},
	}
	clientConfig.HTTPClient = &http.Client{Transport: tr}

	apiClient := pingfederate.NewAPIClient(clientConfig)

	// Set Basic Auth credentials in the Go context
	basicAuthContext := context.WithValue(context.Background(), pingfederate.ContextBasicAuth, pingfederate.BasicAuth{
		UserName: pfUsername,
		Password: pfPassword,
	})

	// Call an API from the `apiClient` object
	readResponse, httpResponse, err := apiClient.IdpAdaptersAPI.GetIdpAdapters(basicAuthContext).Execute()
	if err != nil {
		panic(err)
	}

	fmt.Printf("Response HTTP Code: %d", httpResponse.StatusCode)
	fmt.Printf("Response: %v", readResponse)
}