warrant

package module
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2023 License: MIT Imports: 10 Imported by: 0

README

Warrant Go Library

Use Warrant in server-side Go projects.

Slack

Installation

go get github.com/warrant-dev/warrant-go

Usage

You can use the Warrant SDK with or without a client. Instantiating a client allows you to create different client instances each with their own config (API key, API endpoint, etc).

Without a Client
import "github.com/warrant-dev/warrant-go"

// Setup
warrant.ApiKey = "api_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E="

// Create warrant
warrant, err := warrant.Create(&warrant.WarrantParams{})

// Create tenant
tenant, err := tenant.Create(&tenant.TenantParams{})
With a Client

Instantiate the Warrant client with your API key to get started:

import "github.com/warrant-dev/warrant-go"

client := warrant.NewClient(config.ClientConfig{
    ApiKey: "api_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E=",
	ApiEndpoint: "https://api.warrant.dev",
	AuthorizeEndpoint: "https://api.warrant.dev",
	SelfServiceDashEndpoint: "https://self-serve.warrant.dev"
})

Examples

Users
// Create
createdUser, err := user.Create(&warrant.UserParams{
    UserId: "userId",
})

// Get
user, err := user.Get("userId")


// Delete
err = user.Delete("userId")
Warrants

// Create
createdWarrant, err := warrant.Create(&warrant.WarrantParams{
	ObjectType: "tenant",
	ObjectId:   "1",
	Relation:   "member",
	Subject: warrant.Subject{
		ObjectType: "user",
		ObjectId:   "1",
	},
})

// Delete
err = warrant.Delete(&warrant.WarrantParams{
	ObjectType: "tenant",
	ObjectId:   "1",
	Relation:   "member",
	Subject: warrant.Subject{
		ObjectType: "user",
		ObjectId:   "1",
	},
})

// Check access
isAuthorized, err := warrant.Check(&warrant.WarrantCheckParams{
	Object: warrant.Object{
		ObjectType: "tenant",
		ObjectId:   "1",
	},
	Relation: "member",
	Subject: warrant.Subject{
		ObjectType: "user",
		ObjectId:   "1",
	},
})

We’ve used a random API key in these code examples. Replace it with your actual publishable API keys to test this code through your own Warrant account.

For more information on how to use the Warrant API, please refer to the Warrant API reference.

Note that we may release new minor and patch versions of this library with small but backwards-incompatible fixes to the type declarations. These changes will not affect Warrant itself.

Warrant Documentation

Documentation

Index

Constants

View Source
const (
	SelfServiceStrategyFGAC = "fgac"
	SelfServiceStrategyRBAC = "rbac"
)

Variables

View Source
var ApiEndpoint string = "https://api.warrant.dev"
View Source
var ApiKey string
View Source
var AuthorizeEndpoint string = "https://api.warrant.dev"
View Source
var SelfServiceDashEndpoint string = "https://self-serve.warrant.dev"

Functions

func Check

func Check(params *WarrantCheckParams) (bool, error)

func CheckHasFeature

func CheckHasFeature(params *FeatureCheckParams) (bool, error)

func CheckMany

func CheckMany(params *WarrantCheckManyParams) (bool, error)

func CheckUserHasPermission

func CheckUserHasPermission(params *PermissionCheckParams) (bool, error)

func CheckUserHasRole

func CheckUserHasRole(params *RoleCheckParams) (bool, error)

func Delete

func Delete(params *WarrantParams) error

Types

type AccessCheckRequest

type AccessCheckRequest struct {
	Op             string    `json:"op"`
	Warrants       []Warrant `json:"warrants"`
	ConsistentRead bool      `json:"consistentRead,omitempty"`
	Debug          bool      `json:"debug,omitempty"`
}

type AuthorizationSessionParams

type AuthorizationSessionParams struct {
	UserId string `json:"userId"`
	TTL    int64  `json:"ttl"`
}

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient(config config.ClientConfig) Client

func (Client) Check

func (c Client) Check(params *WarrantCheckParams) (bool, error)

func (Client) CheckHasFeature

func (c Client) CheckHasFeature(params *FeatureCheckParams) (bool, error)

func (Client) CheckMany

func (c Client) CheckMany(params *WarrantCheckManyParams) (bool, error)

func (Client) CheckUserHasPermission

func (c Client) CheckUserHasPermission(params *PermissionCheckParams) (bool, error)

func (Client) CheckUserHasRole

func (c Client) CheckUserHasRole(params *RoleCheckParams) (bool, error)

func (Client) Create

func (c Client) Create(params *WarrantParams) (*Warrant, error)

func (Client) Delete

func (c Client) Delete(params *WarrantParams) error

func (Client) Query

func (c Client) Query(queryString string, listParams *ListWarrantParams) (*QueryWarrantResult, error)

type Context

type Context map[string]string

type EnsureHasPermission

type EnsureHasPermission struct {
	// contains filtered or unexported fields
}

func (*EnsureHasPermission) ServeHTTP

func (ehp *EnsureHasPermission) ServeHTTP(w http.ResponseWriter, r *http.Request)

type EnsureHasPermissionOptions

type EnsureHasPermissionOptions struct {
	PermissionId string
	UserId       string
}

type EnsureIsAuthorized

type EnsureIsAuthorized struct {
	// contains filtered or unexported fields
}

func (*EnsureIsAuthorized) ServeHTTP

func (eia *EnsureIsAuthorized) ServeHTTP(w http.ResponseWriter, r *http.Request)

type EnsureIsAuthorizedOptions

type EnsureIsAuthorizedOptions struct {
	ObjectType string
	ObjectId   string
	Relation   string
	UserId     string
}

type Feature

type Feature struct {
	FeatureId string `json:"featureId"`
}

func (Feature) GetObjectId

func (feature Feature) GetObjectId() string

func (Feature) GetObjectType

func (feature Feature) GetObjectType() string

type FeatureCheckParams

type FeatureCheckParams struct {
	FeatureId      string  `json:"featureId"`
	Subject        Subject `json:"subject"`
	Context        Context `json:"context,omitempty"`
	ConsistentRead bool    `json:"consistentRead,omitempty"`
	Debug          bool    `json:"debug,omitempty"`
}

type FeatureParams

type FeatureParams struct {
	FeatureId string `json:"featureId"`
}

type GetObjectIdFunc

type GetObjectIdFunc func(r *http.Request) string

type GetUserIdFunc

type GetUserIdFunc func(r *http.Request) string

type ListFeatureParams

type ListFeatureParams struct {
	ListParams
}

type ListParams

type ListParams struct {
	Page  int `json:"page"`
	Limit int `json:"limit"`
}

type ListPermissionParams

type ListPermissionParams struct {
	ListParams
}

type ListPricingTierParams

type ListPricingTierParams struct {
	ListParams
}

type ListRoleParams

type ListRoleParams struct {
	ListParams
}

type ListTenantParams

type ListTenantParams struct {
	ListParams
}

type ListUserParams

type ListUserParams struct {
	ListParams
}

type ListWarrantParams

type ListWarrantParams struct {
	ListParams
}

type Middleware

type Middleware struct {
	// contains filtered or unexported fields
}

func NewMiddleware

func NewMiddleware(middlewareConfig MiddlewareConfig) *Middleware

func (Middleware) NewEnsureHasPermission

func (mw Middleware) NewEnsureHasPermission(handler http.Handler, options EnsureHasPermissionOptions) *EnsureHasPermission

func (Middleware) NewEnsureIsAuthorized

func (mw Middleware) NewEnsureIsAuthorized(handler http.Handler, options EnsureIsAuthorizedOptions) *EnsureIsAuthorized

type MiddlewareConfig

type MiddlewareConfig struct {
	ApiKey         string
	GetObjectId    GetObjectIdFunc
	GetUserId      GetUserIdFunc
	OnAccessDenied http.HandlerFunc
}

type NewEnsureHasPermissionFunc

type NewEnsureHasPermissionFunc func(handler http.Handler, options EnsureHasPermissionOptions) *EnsureHasPermission

type NewEnsureIsAuthorizedFunc

type NewEnsureIsAuthorizedFunc func(handler http.Handler, options EnsureIsAuthorizedOptions) *EnsureIsAuthorized

type Object

type Object struct {
	ObjectType string `json:"objectType"`
	ObjectId   string `json:"objectId"`
}

func (Object) GetObjectId

func (object Object) GetObjectId() string

func (Object) GetObjectType

func (object Object) GetObjectType() string

type Permission

type Permission struct {
	PermissionId string `json:"permissionId"`
	Name         string `json:"name,omitempty"`
	Description  string `json:"description,omitempty"`
}

func (Permission) GetObjectId

func (permission Permission) GetObjectId() string

func (Permission) GetObjectType

func (permission Permission) GetObjectType() string

type PermissionCheckParams

type PermissionCheckParams struct {
	PermissionId   string  `json:"permissionId"`
	UserId         string  `json:"userId"`
	Context        Context `json:"context,omitempty"`
	ConsistentRead bool    `json:"consistentRead,omitempty"`
	Debug          bool    `json:"debug,omitempty"`
}

type PermissionParams

type PermissionParams struct {
	PermissionId string `json:"permissionId"`
	Name         string `json:"name,omitempty"`
	Description  string `json:"description,omitempty"`
}

type PricingTier

type PricingTier struct {
	PricingTierId string `json:"pricingTierId"`
}

func (PricingTier) GetObjectId

func (pricingTier PricingTier) GetObjectId() string

func (PricingTier) GetObjectType

func (pricingTier PricingTier) GetObjectType() string

type PricingTierParams

type PricingTierParams struct {
	PricingTierId string `json:"pricingTierId"`
}

type QueryWarrantResult

type QueryWarrantResult struct {
	Result interface{}            `json:"result"`
	Meta   map[string]interface{} `json:"meta"`
}

func Query

func Query(queryString string, params *ListWarrantParams) (*QueryWarrantResult, error)

type Role

type Role struct {
	RoleId      string `json:"roleId"`
	Name        string `json:"name,omitempty"`
	Description string `json:"description,omitempty"`
}

func (Role) GetObjectId

func (role Role) GetObjectId() string

func (Role) GetObjectType

func (role Role) GetObjectType() string

type RoleCheckParams

type RoleCheckParams struct {
	RoleId         string  `json:"roleId"`
	UserId         string  `json:"userId"`
	Context        Context `json:"context,omitempty"`
	ConsistentRead bool    `json:"consistentRead,omitempty"`
	Debug          bool    `json:"debug,omitempty"`
}

type RoleParams

type RoleParams struct {
	RoleId      string `json:"roleId"`
	Name        string `json:"name,omitempty"`
	Description string `json:"description,omitempty"`
}

type SelfServiceSessionParams

type SelfServiceSessionParams struct {
	UserId              string `json:"userId"`
	TenantId            string `json:"tenantId"`
	TTL                 int64  `json:"ttl,omitempty"`
	SelfServiceStrategy string `json:"selfServiceStrategy"`
	ObjectType          string `json:"objectType"`
	ObjectId            string `json:"objectId"`
	RedirectUrl         string `json:"redirectUrl"`
}

type Session

type Session struct {
	UserId   string `json:"userId"`
	TenantId string `json:"tenantId"`
	TTL      int64  `json:"ttl"`
}

type Subject

type Subject struct {
	ObjectType string `json:"objectType"`
	ObjectId   string `json:"objectId"`
	Relation   string `json:"relation,omitempty"`
}

func (Subject) GetObjectId

func (subject Subject) GetObjectId() string

func (Subject) GetObjectType

func (subject Subject) GetObjectType() string

type Tenant

type Tenant struct {
	TenantId  string    `json:"tenantId"`
	Name      string    `json:"name,omitempty"`
	CreatedAt time.Time `json:"createdAt"`
}

func (Tenant) GetObjectId

func (tenant Tenant) GetObjectId() string

func (Tenant) GetObjectType

func (tenant Tenant) GetObjectType() string

type TenantParams

type TenantParams struct {
	TenantId string `json:"tenantId,omitempty"`
	Name     string `json:"name,omitempty"`
}

type User

type User struct {
	UserId string `json:"userId"`
	Email  string `json:"email,omitempty"`
}

func (User) GetObjectId

func (user User) GetObjectId() string

func (User) GetObjectType

func (user User) GetObjectType() string

type UserParams

type UserParams struct {
	UserId string `json:"userId,omitempty"`
	Email  string `json:"email,omitempty"`
}

type Warrant

type Warrant struct {
	ObjectType string  `json:"objectType"`
	ObjectId   string  `json:"objectId"`
	Relation   string  `json:"relation"`
	Subject    Subject `json:"subject"`
	Context    Context `json:"context,omitempty"`
	IsImplicit bool    `json:"isImplicit,omitempty"`
}

func Create

func Create(params *WarrantParams) (*Warrant, error)

type WarrantCheck

type WarrantCheck struct {
	Object   WarrantObject `json:"object"`
	Relation string        `json:"relation"`
	Subject  WarrantObject `json:"subject"`
	Context  Context       `json:"context,omitempty"`
}

func (WarrantCheck) ToWarrant

func (warrantCheck WarrantCheck) ToWarrant() Warrant

type WarrantCheckManyParams

type WarrantCheckManyParams struct {
	Op             string         `json:"op"`
	Warrants       []WarrantCheck `json:"warrants"`
	ConsistentRead bool           `json:"consistentRead,omitempty"`
	Debug          bool           `json:"debug,omitempty"`
}

type WarrantCheckParams

type WarrantCheckParams struct {
	WarrantCheck   WarrantCheck `json:"warrantCheck"`
	ConsistentRead bool         `json:"consistentRead,omitempty"`
	Debug          bool         `json:"debug,omitempty"`
}

type WarrantCheckResult

type WarrantCheckResult struct {
	Code   int64  `json:"code"`
	Result string `json:"result"`
}

type WarrantObject

type WarrantObject interface {
	GetObjectType() string
	GetObjectId() string
}

type WarrantParams

type WarrantParams struct {
	ObjectType string  `json:"objectType"`
	ObjectId   string  `json:"objectId"`
	Relation   string  `json:"relation"`
	Subject    Subject `json:"subject"`
	Context    Context `json:"context,omitempty"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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