warrant

package module
v5.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2023 License: MIT Imports: 9 Imported by: 0

README

Warrant Go Library

Use Warrant in server-side Go projects.

Slack

Installation

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

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/v5"

// 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/v5"
import "github.com/warrant-dev/warrant-go/v5/config"

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",
})

Configuring Endpoints

The API, Authorize, Self-Service endpoints and http client are configurable via the warrant.ApiEndpoint, warrant.AuthorizeEndpoint, warrant.SelfServiceDashEndpoint, and warrant.HttpClient attributes:

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

// Without client initialization
// Set api and authorize endpoints to http://localhost:8000
// Set http client to a http.Client instance returned by yourHttpClient()
warrant.ApiEndpoint = "http://localhost:8000"
warrant.AuthorizeEndpoint = "http://localhost:8000"
warrant.HttpClient = yourHttpClient()

// With client initialization
// Set api and authorize endpoints to http://localhost:8000 and self-service endpoint to http://localhost:8080
// Set http client to a http.Client instance returned by yourHttpClient()
client := warrant.NewClient(config.ClientConfig{
	ApiKey: "api_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E=",
	ApiEndpoint: "http://localhost:8000",
	AuthorizeEndpoint: "http://localhost:8000",
	SelfServiceDashEndpoint: "http://localhost:8080",
	HttpClient: yourHttpClient(),
})

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"
)
View Source
const (
	ClientVersion string = "5.3.0"
)
View Source
const ObjectTypeFeature = "feature"
View Source
const ObjectTypePermission = "permission"
View Source
const ObjectTypePricingTier = "pricing-tier"
View Source
const ObjectTypeRole = "role"
View Source
const ObjectTypeTenant = "tenant"
View Source
const ObjectTypeUser = "user"

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 {
	RequestOptions
	Op       string         `json:"op"`
	Warrants []WarrantCheck `json:"warrants"`
	Debug    bool           `json:"debug,omitempty"`
}

type ApiClient

type ApiClient struct {
	HttpClient *http.Client
	Config     ClientConfig
}

func NewApiClient

func NewApiClient(config ClientConfig) *ApiClient

func (ApiClient) MakeRequest

func (client ApiClient) MakeRequest(method string, path string, payload interface{}, options *RequestOptions) (*http.Response, error)

type AuthorizationSessionParams

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

type ClientConfig

type ClientConfig struct {
	ApiKey                  string
	ApiEndpoint             string
	AuthorizeEndpoint       string
	SelfServiceDashEndpoint string
	HttpClient              *http.Client
}

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 Error

type Error struct {
	Message      string `json:"message"`
	WrappedError error  `json:"-"`
}

func WrapError

func WrapError(message string, err error) Error

func (Error) Error

func (err Error) Error() 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 {
	RequestOptions
	FeatureId string        `json:"featureId"`
	Subject   Subject       `json:"subject"`
	Context   PolicyContext `json:"context,omitempty"`
	Debug     bool          `json:"debug,omitempty"`
}

type FeatureParams

type FeatureParams struct {
	RequestOptions
	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 ListObjectParams added in v5.1.0

type ListObjectParams struct {
	ListParams
}

type ListObjectTypeParams added in v5.1.0

type ListObjectTypeParams struct {
	ListParams
}

type ListParams

type ListParams struct {
	RequestOptions
	BeforeId    string `json:"beforeId"`
	BeforeValue string `json:"beforeValue"`
	AfterId     string `json:"afterId"`
	AfterValue  string `json:"afterValue"`
	SortBy      string `json:"sortBy"`
	SortOrder   string `json:"sortOrder"`
	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"`
	Meta       map[string]interface{} `json:"meta"`
}

func (Object) GetObjectId

func (object Object) GetObjectId() string

func (Object) GetObjectType

func (object Object) GetObjectType() string

type ObjectParams added in v5.1.0

type ObjectParams struct {
	RequestOptions
	ObjectType string                 `json:"objectType"`
	ObjectId   string                 `json:"objectId,omitempty"`
	Meta       map[string]interface{} `json:"meta,omitempty"`
}

type ObjectType added in v5.1.0

type ObjectType struct {
	Type      string                 `json:"type"`
	Relations map[string]interface{} `json:"relations"`
}

type ObjectTypeParams added in v5.1.0

type ObjectTypeParams struct {
	RequestOptions
	Type      string                 `json:"type"`
	Relations map[string]interface{} `json:"relations"`
}

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 {
	RequestOptions
	PermissionId string        `json:"permissionId"`
	UserId       string        `json:"userId"`
	Context      PolicyContext `json:"context,omitempty"`
	Debug        bool          `json:"debug,omitempty"`
}

type PermissionParams

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

type PolicyContext

type PolicyContext map[string]interface{}

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 {
	RequestOptions
	PricingTierId string `json:"pricingTierId"`
}

type QueryParams added in v5.2.0

type QueryParams struct {
	ListParams
	// contains filtered or unexported fields
}

type QueryResponse added in v5.2.0

type QueryResponse struct {
	Results []QueryResult `json:"results"`
	LastId  *string       `json:"lastId,omitempty"`
}

func Query

func Query(queryString string, params *QueryParams) (*QueryResponse, error)

type QueryResult added in v5.2.0

type QueryResult struct {
	ObjectType string                 `json:"objectType"`
	ObjectId   string                 `json:"objectId"`
	Warrant    Warrant                `json:"warrant"`
	IsImplicit bool                   `json:"isImplicit"`
	Meta       map[string]interface{} `json:"meta"`
}

type RequestOptions

type RequestOptions struct {
	WarrantToken string
}

func (*RequestOptions) SetWarrantToken

func (requestOptions *RequestOptions) SetWarrantToken(token string)

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 {
	RequestOptions
	RoleId  string        `json:"roleId"`
	UserId  string        `json:"userId"`
	Context PolicyContext `json:"context,omitempty"`
	Debug   bool          `json:"debug,omitempty"`
}

type RoleParams

type RoleParams struct {
	RequestOptions
	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 {
	RequestOptions
	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 {
	RequestOptions
	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"`
	Policy     string  `json:"policy,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  PolicyContext `json:"context,omitempty"`
}

func (WarrantCheck) MarshalJSON

func (warrantCheck WarrantCheck) MarshalJSON() ([]byte, error)

type WarrantCheckManyParams

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

type WarrantCheckParams

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

type WarrantCheckResult

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

type WarrantClient

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

func NewClient

func NewClient(config ClientConfig) WarrantClient

func (WarrantClient) Check

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

func (WarrantClient) CheckHasFeature

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

func (WarrantClient) CheckMany

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

func (WarrantClient) CheckUserHasPermission

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

func (WarrantClient) CheckUserHasRole

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

func (WarrantClient) Create

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

func (WarrantClient) Delete

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

func (WarrantClient) Query

func (c WarrantClient) Query(queryString string, params *QueryParams) (*QueryResponse, error)

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"`
	Policy     string  `json:"policy,omitempty"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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