identity

package module
v3.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2022 License: Apache-2.0 Imports: 12 Imported by: 0

README

Tanker logo

Identity SDK

Actions Status codecov GoDoc

Identity generation in Go for the Tanker SDK.

Installation

go get github.com/TankerHQ/identity-go/v3

Usage

The server-side code below demonstrates a typical flow to safely deliver identities to your users:

import (
	"fmt"
	"errors"

	"github.com/TankerHQ/identity-go/v3"
)

var config = identity.Config{
	AppID:     "<app-id>",
	AppSecret: "<app-secret>",
}

// Example server-side function in which you would implement checkAuth(),
// retrieveIdentity() and storeIdentity() to use your own authentication
// and data storage mechanisms:
func getIdentity(userID string) (*string, error) {
	// Always ensure userID is authenticated before returning a identity
	if !isAuthenticated(userID) {
		return nil, errors.New("Unauthorized")
	}

	// Retrieve a previously stored identity for this user
	identity := retrieveIdentity(userID)

	// If not found, create a new identity
	if identity == "" {
		identity, err := identity.Create(config, userID)
		if err != nil {
			return nil, err
		}

		// Store the newly generated identity
		storeIdentity(userID, identity)
	}

	// From now, the same identity will always be returned to a given user
	return &identity, nil
}

func getPublicIdentity(userID string) (*string, error) {
	// Retrieve a previously stored identity for this user
	tkIdentity := retrieveIdentity(userID)
	if tkIdentity == "" {
		return nil, errors.New("Not found")
	}

	publicIdentity, err := identity.GetPublicIdentity(tkIdentity)
	if err != nil {
		return nil, err
	}

	return publicIdentity, nil
}

Read more about identities in the Tanker guide.

Development

Run tests:

go test ./... -test.v

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/TankerHQ/identity-go.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Create

func Create(config Config, userID string) (*string, error)

Create returns a new identity crafted from config and userID

func CreateProvisional

func CreateProvisional(config Config, target string, value string) (*string, error)

CreateProvisional returns a new provisional identity crafted from config, target and value

func Decode

func Decode(b64 string, v interface{}) error

Decode takes a value typically returned by Encode, that is, a base64-encoded JSON-marshalled value, and applies the reverse operation, first base64-decoding b64, then unmarshalling the resulting JSON representation into v. If an error occurs on the way, it is returned.

func Encode

func Encode(v interface{}) (*string, error)

Encode returns a pointer to the base64 representation of the result of marshalling v in JSON. If an error occurs in the process, it is returned. Under the hood, Encode transforms v into a *orderedmap.OrderedMap so the result will always wrap a key-sorted JSON representation. If v's underlying type is already *orderedmap.OrderedMap, v's wrapped value will be used as is.

func GetPublicIdentity

func GetPublicIdentity(b64Identity string) (*string, error)

GetPublicIdentity returns the public identity associated with the provided identity

func UpgradeIdentity

func UpgradeIdentity(b64Identity string) (*string, error)

UpgradeIdentity upgrades the provided identity if needed and returns the result of the upgrade

Types

type Config

type Config struct {
	// AppID is the ID of the app corresponding to this config
	AppID string
	// AppSecret is a secret used to create identities
	AppSecret string
}

Config wraps information about an app

Directories

Path Synopsis
internal
app

Jump to

Keyboard shortcuts

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