The Dex API

Dex provides a gRPCservice for programmatic modification of dex’s state. The API is intended to expose hooks for management applications and is not expected to be used by most installations.

This document is an overview of how to interact with the API.

Configuration

Admins that wish to expose the gRPC service must add the following entry to the dex config file. This option is off by default.

  1. grpc:
  2. # Cannot be the same address as an HTTP(S) service.
  3. addr: 127.0.0.1:5557
  4. # Server certs. If TLS credentials aren't provided dex will run in plaintext (HTTP) mode.
  5. tlsCert: /etc/dex/grpc.crt
  6. tlsKey: /etc/dex/grpc.key
  7. # Client auth CA.
  8. tlsClientCA: /etc/dex/client.crt
  9. # enable reflection
  10. reflection: true

Clients

gRPC is a suite of tools for generating client and server bindings from a common declarative language. The canonical schema for Dex’s API can be found in the source tree at api/v2/api.proto. Go bindings are generated and maintained in the same directory for both public and internal use.

Go

A Go project can import the API module directly, without having to import the entire project:

  1. go get github.com/dexidp/dex/api/v2

The client then can be used as follows:

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "github.com/dexidp/dex/api/v2"
  7. "google.golang.org/grpc"
  8. "google.golang.org/grpc/credentials"
  9. )
  10. func newDexClient(hostAndPort, caPath string) (api.DexClient, error) {
  11. creds, err := credentials.NewClientTLSFromFile(caPath, "")
  12. if err != nil {
  13. return nil, fmt.Errorf("load dex cert: %v", err)
  14. }
  15. conn, err := grpc.Dial(hostAndPort, grpc.WithTransportCredentials(creds))
  16. if err != nil {
  17. return nil, fmt.Errorf("dial: %v", err)
  18. }
  19. return api.NewDexClient(conn), nil
  20. }
  21. func main() {
  22. client, err := newDexClient("127.0.0.1:5557", "/etc/dex/grpc.crt")
  23. if err != nil {
  24. log.Fatalf("failed creating dex client: %v ", err)
  25. }
  26. req := &api.CreateClientReq{
  27. Client: &api.Client{
  28. Id: "example-app",
  29. Name: "Example App",
  30. Secret: "ZXhhbXBsZS1hcHAtc2VjcmV0",
  31. RedirectUris: []string{"http://127.0.0.1:5555/callback"},
  32. },
  33. }
  34. if _, err := client.CreateClient(context.TODO(), req); err != nil {
  35. log.Fatalf("failed creating oauth2 client: %v", err)
  36. }
  37. }

A clear working example of the Dex gRPC client for Go can be found here.

Other languages

To generate a client for your own project install protoc, install a protobuf generator for your project’s language, and download the api.proto file.

Here is an example:

  1. # Download api.proto for a given version.
  2. $ DEX_VERSION=v2.24.0
  3. $ wget https://raw.githubusercontent.com/dexidp/dex/${DEX_VERSION}/api/v2/api.proto
  4. # Generate the client bindings.
  5. $ protoc [YOUR LANG PARAMS] api.proto

Client programs can then be written using the generated code.

Authentication and access control

The Dex API does not provide any authentication or authorization beyond TLS client auth.

Projects that wish to add access controls on top of the existing API should build apps which perform such checks. For example to provide a “Change password” screen, a client app could use Dex’s OpenID Connect flow to authenticate an end user, then call Dex’s API to update that user’s password.

dexctl?

Dex does not ship with a command line tool for interacting with the API. Command line tools are useful but hard to version, easy to design poorly, and expose another interface which can never be changed in the name of compatibility.

While the Dex team would be open to re-implementing dexctl for v2 a majority of the work is writing a design document, not the actual programming effort.

Why not REST or gRPC Gateway?

Between v1 and v2, Dex switched from REST to gRPC. This largely stemmed from problems generating documentation, client bindings, and server frameworks that adequately expressed REST semantics. While Google APIs, Open API/Swagger, and gRPC Gatewaywere evaluated, they often became clunky when trying to use specific HTTP error codes or complex request bodies. As a result, v2’s API is entirely gRPC.

Many arguments against gRPC cite short term convenience rather than production use cases. Though this is a recognized shortcoming, Dex already implements many features for developer convenience. For instance, users who wish to manually edit clients during testing can use the staticClients config field instead of the API.