auth0

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2023 License: MIT Imports: 2 Imported by: 0

README

Go SDK for Auth0

GoDoc Go Report Card Release License Build Status Codecov FOSSA Status

📚 Documentation • 🚀 Getting Started • 💬 Feedback


Documentation

  • Godoc - explore the Go SDK documentation.
  • Docs site — explore our docs site and learn more about Auth0.
  • Examples - Further examples around usage of the SDK.

Getting started

Requirements

This library follows the same support policy as Go. The last two major Go releases are actively supported and compatibility issues will be fixed. While you may find that older versions of Go may work, we will not actively test and fix compatibility issues with these versions.

  • Go 1.20+

Installation

go get github.com/ConsultingMD/go-auth0

Usage

Authentication API Client

The Authentication API client is based on the Authentication API docs.

Create an Authentication API client by providing the details of your Auth0 Application.

package main

import (
	"context"
	"log"

	"github.com/ConsultingMD/go-auth0/authentication"
	"github.com/ConsultingMD/go-auth0/authentication/database"
	"github.com/ConsultingMD/go-auth0/authentication/oauth"
)

func main() {
	// Get these from your Auth0 Application Dashboard.
	domain := "example.us.auth0.com"
	clientID := "EXAMPLE_16L9d34h0qe4NVE6SaHxZEid"
	clientSecret := "EXAMPLE_XSQGmnt8JdXs23407hrK6XXXXXXX"

	// Initialize a new client using a domain, client ID and client secret.
	authAPI, err := authentication.New(
		context.Background(),
		domain,
		authentication.WithClientID(clientID),
		authentication.WithClientSecret(clientSecret), // Optional depending on the grants used
	)
	if err != nil {
		log.Fatalf("failed to initialize the auth0 authentication API client: %+v", err)
	}

	// Now we can interact with the Auth0 Authentication API.
	// Sign up a user
	userData := database.SignupRequest{
		Connection: "Username-Password-Authentication",
		Username:   "mytestaccount",
		Password:   "mypassword",
		Email:      "mytestaccount@example.com",
	}

	createdUser, err := authAPI.Database.Signup(context.Background(), userData)
	if err != nil {
		log.Fatalf("failed to sign user up: %+v", err)
	}

	// Login using OAuth grants
	tokenSet, err := authAPI.OAuth.LoginWithAuthCodeWithPKCE(context.Background(), oauth.LoginWithAuthCodeWithPKCERequest{
		Code:         "test-code",
		CodeVerifier: "test-code-verifier",
	}, oauth.IDTokenValidationOptions{})
	if err != nil {
		log.Fatalf("failed to retrieve tokens: %+v", err)
	}
}

Note The context package can be used to pass cancellation signals and deadlines to the Client for handling a request. If there is no context available then context.Background() can be used.

Management API Client

The Management API client is based on the Management API docs.

package main

import (
	"context"
	"log"

	"github.com/ConsultingMD/go-auth0"
	"github.com/ConsultingMD/go-auth0/management"
)

func main() {
	// Get these from your Auth0 Application Dashboard.
	// The application needs to be a Machine To Machine authorized
	// to request access tokens for the Auth0 Management API,
	// with the desired permissions (scopes).
	domain := "example.auth0.com"
	clientID := "EXAMPLE_16L9d34h0qe4NVE6SaHxZEid"
	clientSecret := "EXAMPLE_XSQGmnt8JdXs23407hrK6XXXXXXX"

	// Initialize a new client using a domain, client ID and client secret.
	// Alternatively you can specify an access token:
	// `management.WithStaticToken("token")`
	auth0API, err := management.New(
		domain,
		management.WithClientCredentials(context.Background(), clientID, clientSecret),
	)
	if err != nil {
		log.Fatalf("failed to initialize the auth0 management API client: %+v", err)
	}

	// Now we can interact with the Auth0 Management API.
	// Example: Creating a new client.
	client := &management.Client{
		Name:        auth0.String("My Client"),
		Description: auth0.String("Client created through the Go SDK"),
	}

	// The passed in client will get hydrated with the response.
	// This means that after this request, we will have access
	// to the client ID on the same client object.
	err = auth0API.Client.Create(context.Background(), client)
	if err != nil {
		log.Fatalf("failed to create a new client: %+v", err)
	}

	// Make use of the getter functions to safely access
	// fields without causing a panic due nil pointers.
	log.Printf(
		"Created an auth0 client successfully. The ID is: %q",
		client.GetClientID(),
	)
}

Note The context package can be used to pass cancellation signals and deadlines to the Client for handling a request. If there is no context available then context.Background() can be used.

Rate Limiting

The Auth0 Management API imposes a rate limit on all API clients. When the limit is reached, the SDK will handle it in the background by retrying the API request when the limit is lifted.

Note The SDK does not prevent http.StatusTooManyRequests errors, instead it waits for the rate limit to be reset based on the value of the X-Rate-Limit-Reset header as the amount of seconds to wait.

Feedback

Contributing

We appreciate feedback and contribution to this repo! Before you get started, please see the following:

Raise an issue

To provide feedback or report a bug, please raise an issue on our issue tracker.

Vulnerability Reporting

Please do not report security vulnerabilities on the public Github issue tracker. The Responsible Disclosure Program details the procedure for disclosing security issues.


Auth0 Logo

Auth0 is an easy to implement, adaptable authentication and authorization platform.
To learn more checkout Why Auth0?

This project is licensed under the MIT license. See the LICENSE file for more info.

Documentation

Overview

Package auth0 provides a client for using the Auth0 Authentication and Management APIs.

Authentication

Usage

import (
	"github.com/ConsultingMD/go-auth0"
	"github.com/ConsultingMD/go-auth0/authentication"
	"github.com/ConsultingMD/go-auth0/authentication/database"
	"github.com/ConsultingMD/go-auth0/authentication/oauth"
)

Initialize a new client using a context, domain, client ID, and client secret if required.

authAPI, err := authentication.New(
	context.Background(),
	domain,
	authentication.WithClientID(id),
	authentication.WithClientSecret(secret), // Optional depending on the grants used
)
if err != nil {
	// handle err
}

Now we have an authentication client, we can interact with the Auth0 Authentication API.

// Sign up a user
userData := database.SignupRequest{
	Connection: "Username-Password-Authentication",
	Username:   "mytestaccount",
	Password:   "mypassword",
	Email:      "mytestaccount@example.com",
}

createdUser, err := authAPI.Database.Signup(context.Background(), userData)
if err != nil {
	// handle err
}

// Login using OAuth grants
tokenSet, err := authAPI.OAuth.LoginWithAuthCodeWithPKCE(context.Background(), oauth.LoginWithAuthCodeWithPKCERequest{
	Code:         "test-code",
	CodeVerifier: "test-code-verifier",
}, oauth.IDTokenValidationOptionalVerification{})
if err != nil {
	// handle err
}

Management

Usage

import (
	"github.com/ConsultingMD/go-auth0"
	"github.com/ConsultingMD/go-auth0/management"
)

Initialize a new client using a domain, client ID and secret.

m, err := management.New(
	domain,
	management.WithClientCredentials(context.Background(), id, secret),
)
if err != nil {
	// handle err
}

Or using a static token.

m, err := management.New(domain, management.WithStaticToken(token))
if err != nil {
	// handle err
}

With a management client we can then interact with the Auth0 Management API.

c := &management.Client{
	Name:        auth0.String("Client Name"),
	Description: auth0.String("Long description of client"),
}

err = m.Client.Create(context.Background(), c)
if err != nil {
	// handle err
}

## Authentication

The auth0 management package handles authentication by exchanging the client ID and secret supplied when creating a new management client.

This is handled internally using the https://godoc.org/golang.org/x/oauth2 package.

## Rate Limiting

The auth0 package also handles rate limiting by respecting the `X-Rate-Limit-*` headers sent by the server.

The amount of time the client waits for the rate limit to be reset is taken from the `X-Rate-Limit-Reset` header as the amount of seconds to wait.

Configuration

There are several other options that can be specified during the creation of a new client.

m, err := management.New(
	domain,
	management.WithClientCredentials(context.Background(), id, secret),
	management.WithDebug(true),
)

## Request Options

As with the global client configuration, fine-grained configuration can be done on a request basis.

c, err := m.Connection.List(
	context.Background(),
	management.Page(2),
	management.PerPage(10),
	management.IncludeFields("id", "name", "options"),
	management.Parameter("strategy", "auth0"),
)

Index

Constants

This section is empty.

Variables

View Source
var Version = "latest"

Version of this library. This value is generated automatically during the release process; DO NOT EDIT.

Functions

func Bool

func Bool(b bool) *bool

Bool returns a pointer to the bool value passed in.

func BoolValue

func BoolValue(b *bool) bool

BoolValue returns the value of the bool pointer passed in or false if the pointer is nil.

func Float64

func Float64(f float64) *float64

Float64 returns a pointer to the float64 value passed in.

func Float64Value

func Float64Value(f *float64) float64

Float64Value returns the value of the float64 pointer passed in or 0 if the pointer is nil.

func Int

func Int(i int) *int

Int returns a pointer to the int value passed in.

func IntValue

func IntValue(i *int) int

IntValue returns the value of the int pointer passed in or 0 if the pointer is nil.

func String

func String(s string) *string

String returns a pointer to the string value passed in.

func StringValue

func StringValue(v *string) string

StringValue returns the value of the string pointer passed in or "" if the pointer is nil.

func Stringf

func Stringf(s string, v ...interface{}) *string

Stringf returns a pointer to the string value passed in formatted using fmt.Sprintf.

func Time

func Time(t time.Time) *time.Time

Time returns a pointer to the time value passed in.

func TimeValue

func TimeValue(t *time.Time) time.Time

TimeValue returns the value of the time pointer passed in or the zero value of time if the pointer is nil.

Types

This section is empty.

Directories

Path Synopsis
internal
tag

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL