GithubHelp home page GithubHelp logo

weirdian2k3 / go-auth0 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from auth0/go-auth0

0.0 0.0 0.0 1.75 MB

Go SDK for the Auth0 Management API.

Home Page: https://auth0.com

License: MIT License

Shell 0.19% Go 99.24% Makefile 0.57%

go-auth0's Introduction

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.21+

Installation

go get github.com/auth0/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/auth0/go-auth0/authentication"
	"github.com/auth0/go-auth0/authentication/database"
	"github.com/auth0/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.TODO(), // Replace with a Context that better suits your usage
		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:      "[email protected]",
	}

	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/auth0/go-auth0"
	"github.com/auth0/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.TODO(), clientID, clientSecret),  // Replace with a Context that better suits your usage
	)
	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.TODO(), client)  // Replace with a Context that better suits your usage
	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.

go-auth0's People

Contributors

alexkappa avatar sergiught avatar ewanharris avatar dependabot[bot] avatar yvovandoorn avatar widcket avatar kgunbin avatar yinzara avatar sethyates avatar evansims avatar apamildner avatar willvedd avatar lambdalisue avatar mcalster avatar cyx avatar gagalago avatar simonbarendse avatar apricote avatar weirdian2k3 avatar epintos avatar mattoddie avatar hypnoglow avatar rene00 avatar mkusaka avatar jmaeso avatar bishtawi avatar shushen avatar nishanths avatar nialdaly avatar mlafeldt avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.