auth

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2021 License: MIT Imports: 7 Imported by: 0

README

A package for authentication via AWS Cognito

GitHub tag (latest SemVer pre-release) GitHub go.mod Go version GitHub Workflow Status Go Report Card PkgGoDev

Introduction

  • Authenticate users via AWS Cognito

Example

package main

import (
	"context"

	auth "github.com/gofor-little/aws-auth"
)

func main() {
	// Initialize the auth package.
	if err := auth.Initialize(context.Background(), "AWS_PROFILE", "AWS_REGION", "USER_POOL_ID", "CLIENT_ID"); err != nil {
		panic(err)
	}

	// Sign in.
	if _, err := auth.SignIn(context.Background(), "john@example.com", "password1234"); err != nil {
		panic(err)
	}
}

Testing

Ensure the following environment variables are set, usually with a .env file.

  • AWS_PROFILE (an AWS CLI profile name)
  • AWS_REGION (a valid AWS region)

Run go test ./... at the root of the project.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// CognitoClient is the client that interacts with Cognito.
	CognitoClient *cognitoidentityprovider.Client
	// CognitoUserPoolID is the id of the user pool in Cognito.
	CognitoUserPoolID string
	// CognitoClientID is the id of the user pool client in Cognito.
	CognitoClientID string
)

Functions

func ChangePassword

func ChangePassword(ctx context.Context, accessToken string, oldPassword string, newPassword string) error

ChangePassword changes a user's password.

- Use auth.ForgotPassword if the user doesn't know their password.

- Use auth.UpdateExpiredPassword if the user has a requirement for their password to be changed.

func ForgotPassword

func ForgotPassword(ctx context.Context, emailAddress string) (*cognitoidentityprovider.ForgotPasswordOutput, error)

ForgotPassword will initiate a forgot password request.

- Use auth.ChangePassword and auth.ChangePasswordConfirm to update a user's password that doesn't require resetting.

- Use auth.UpdateExpiredPassword if the user has a requirement for their password to be changed.

func ForgotPasswordConfirm

func ForgotPasswordConfirm(ctx context.Context, confirmationCode string, emailAddress string, newPassword string) error

ForgotPasswordConfirm will confirm a forgot password request.

func Initialize

func Initialize(ctx context.Context, profile string, region string, cognitoUserPoolID string, cognitoClientID string) error

Initialize will initialize the auth package. Both profile and region parameters are option if authentication can be achieved via another method. For example, environment variables or IAM roles.

func SignIn

func SignIn(ctx context.Context, emailAddress string, password string) (*cognitoidentityprovider.InitiateAuthOutput, error)

SignIn will attempt to sign a user in, returning the result.

func SignUp

func SignUp(ctx context.Context, emailAddress string, password string) (string, error)

SignUp signs a new user up.

func SignUpConfirm

func SignUpConfirm(ctx context.Context, emailAddress string, confirmationCode string) error

SignUpConfirm confirms a newly signed up user with the confirmation code they received.

func UpdateExpiredPassword added in v0.4.0

func UpdateExpiredPassword(ctx context.Context, session string, emailAddress string, password string) (*cognitoidentityprovider.RespondToAuthChallengeOutput, error)

UpdateExpiredPassword updates a password for a user that has a requirement for their password to be changed. The session parameter can be obtained from the output.Session return value of auth.SignIn.

- Use auth.ForgotPassword if the user doesn't know their password.

- Use auth.ChangePassword and auth.ChangePasswordConfirm to update a user's password that doesn't require resetting.

Types

This section is empty.

Jump to

Keyboard shortcuts

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