Authentication and Security
The Admin API is protected by cryptographic keys. Learn how to generate a valid JWT to authorize your request in this article.
Accessing the Admin API GraphQL endpoint
Obtaining the private key for signing JWT
Generating the JWT with the private key
Sample code to generate the JWT
package main
import (
"encoding/json"
"fmt"
"os"
"time"
"github.com/lestrrat-go/jwx/v2/jwa"
"github.com/lestrrat-go/jwx/v2/jwk"
"github.com/lestrrat-go/jwx/v2/jws"
"github.com/lestrrat-go/jwx/v2/jwt"
)
// Replace "myapp" with your project ID here.
// It is the first part of your Authgear endpoint.
// e.g. The project ID is "myapp" for "https://myapp.authgear.cloud"
const ProjectID = "myapp"
// Replace "mykid" with the key ID you see in the portal.
const KeyID = "mykid"
func main() {
// Replace the following call with your own way to get the private key.
f, err := os.Open("private-key.pem")
if err != nil {
panic(err)
}
defer f.Close()
jwkSet, err := jwk.ParseReader(f, jwk.WithPEM(true))
if err != nil {
panic(err)
}
key, _ := jwkSet.Key(0)
key.Set("kid", KeyID)
now := time.Now().UTC()
payload := jwt.New()
_ = payload.Set(jwt.AudienceKey, ProjectID)
_ = payload.Set(jwt.IssuedAtKey, now.Unix())
_ = payload.Set(jwt.ExpirationKey, now.Add(5*time.Minute).Unix())
// The alg MUST be RS256.
alg := jwa.RS256
hdr := jws.NewHeaders()
hdr.Set("typ", "JWT")
buf, err := json.Marshal(payload)
if err != nil {
panic(err)
}
token, err := jws.Sign(buf, jws.WithKey(alg, key, jws.WithProtectedHeaders(hdr)))
if err != nil {
panic(err)
}
fmt.Printf("%v\n", string(token))
}Example of the JWT header
Example of the JWT payload
Including the JWT in the HTTP request
Optional: Caching the JWT
Admin API Key rotation
Last updated
Was this helpful?