ucs

package module
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2022 License: Apache-2.0 Imports: 16 Imported by: 0

README

ucs-go-sdk

build Go Reference

用于集成ucs的开发包

快速开始

安装
go get -u github.com/Macrow/ucs-go-sdk
验证Jwt
validator := NewValidator(rsaPublicKey)
jwtUser, err := validator.ValidateJwt(token)
创建连接UCS的客户端
client := NewRpcClient("your.domain.com:port") // Rpc客户端
// client := NewTLSRpcClient(certFile, "your.domain.com:port") // TLS连接下的Rpc客户端,需要UCS服务也同时开启
// client := NewHttpClient("https://your.domain.com:port", yourAccessCode) // Http客户端
client.SetToken(token)
UCS服务端验证Jwt
err := client.ValidateJwt()
UCS服务端验证操作码
err := client.ValidatePermOperationByCode("UCS_O_CODE")
UCS服务端验证接口
err := client.ValidatePermAction("ucs", "/api/v1/ucs/users", "get")
UCS服务端验证用户是否拥有机构权限
err := client.ValidatePermOrgById("org_id_is_here")

Documentation

Index

Constants

View Source
const (
	DefaultHeaderRandomKey  = "Random-Key"
	DefaultHeaderAccessCode = "Access-Code"
	DefaultNoPermMsg        = "权限不足"
	DefaultTimeout          = 3

	ValidateJwtURL                 = "/api/v1/ucs/current/blank"
	ValidatePermOperationByCodeURL = "/api/v1/ucs/current/check-operation"
	ValidatePermActionURL          = "/api/v1/ucs/current/check-action"
	ValidatePermOrgByIdURL         = "/api/v1/ucs/current/check-org"
	ValidateRenewTokenURL          = "/api/v1/ucs/public/renew-token"
)
View Source
const (
	JwtTokenClaimsId         = "id"
	JwtTokenClaimsName       = "name"
	JwtTokenClaimsDeviceId   = "did"
	JwtTokenClaimsDeviceName = "dn"
	JwtTokenClaimsIssuer     = "iss"
	JwtTokenClaimsIssueAt    = "iat"
	JwtTokenClaimsExpireAt   = "exp"
)
View Source
const (
	JwtKeySplitter = "_"
	JwtErrInternal = "内部错误"
	JwtErrFormat   = "令牌格式错误"
	JwtErrVersion  = "令牌版本错误"
)

Variables

This section is empty.

Functions

func GetUserJwtCacheKey added in v1.3.0

func GetUserJwtCacheKey(prefix, userId, deviceId string) string

GetUserJwtCacheKey 获取Jwt存储的key

func IsSame added in v1.3.0

func IsSame(u1 *RawJwtUser, u2 *RawJwtUser) bool

Types

type Client

type Client interface {
	SetTimeout(timeout int) Client
	SetToken(token string) Client
	SetHttpHeaderNames(accessCodeHeader, randomKeyHeader string) Client
	ValidateJwt() error
	ValidatePermOperationByCode(operationCode string) error
	ValidatePermAction(service, path, method string) error
	ValidatePermOrgById(orgId string) error
}

func NewHttpClient added in v1.1.0

func NewHttpClient(baseUrl string, accessCode string) Client

func NewRpcClient added in v1.1.0

func NewRpcClient(addr string) Client

func NewTLSRpcClient added in v1.1.0

func NewTLSRpcClient(cert []byte, addr string) Client

type CommonPermitResult added in v1.1.0

type CommonPermitResult struct {
	Permit bool `json:"permit"`
}

type CommonRenewTokenResult added in v1.3.0

type CommonRenewTokenResult struct {
	Token string `json:"token"`
}

type HttpClient added in v1.1.0

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

func (*HttpClient) SetHttpHeaderNames added in v1.1.0

func (c *HttpClient) SetHttpHeaderNames(accessCodeHeader, randomKeyHeader string) Client

func (*HttpClient) SetTimeout added in v1.1.0

func (c *HttpClient) SetTimeout(timeout int) Client

func (*HttpClient) SetToken added in v1.1.0

func (c *HttpClient) SetToken(token string) Client

func (*HttpClient) ValidateJwt added in v1.1.0

func (c *HttpClient) ValidateJwt() error

func (*HttpClient) ValidatePermAction added in v1.1.0

func (c *HttpClient) ValidatePermAction(service, path, method string) error

func (*HttpClient) ValidatePermOperationByCode added in v1.1.0

func (c *HttpClient) ValidatePermOperationByCode(operationCode string) error

func (*HttpClient) ValidatePermOrgById added in v1.1.0

func (c *HttpClient) ValidatePermOrgById(orgId string) error

type JwtUser

type JwtUser struct {
	RawJwtUser
	Token string `json:"token"` // 令牌字符串
}

func GenerateJwt added in v1.3.0

func GenerateJwt(privateKey []byte, id, username, deviceId, deviceName, issuer string, issueAt float64, expireAt float64) (jwtUser *JwtUser, err error)

func ValidateJwt

func ValidateJwt(publicKey []byte, tokenString string) (*JwtUser, error)

type NormalHttpResponse added in v1.1.0

type NormalHttpResponse struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Result  interface{} `json:"result"`
}

type PermitHttpResponse added in v1.1.0

type PermitHttpResponse struct {
	Code    int                `json:"code"`
	Message string             `json:"message"`
	Result  CommonPermitResult `json:"result"`
}

type RawJwtUser added in v1.3.0

type RawJwtUser struct {
	Id         string  `json:"id"`   // 用户id
	Name       string  `json:"name"` // 用户登录名
	DeviceId   string  `json:"did"`  // 设备id
	DeviceName string  `json:"dn"`   // 设备名
	Issuer     string  `json:"iss"`  // 签发者
	IssueAt    float64 `json:"iat"`  // 签发时间
	ExpireAt   float64 `json:"exp"`  // 过期时间
}

type RenewTokenHttpResponse added in v1.3.0

type RenewTokenHttpResponse struct {
	Code    int                    `json:"code"`
	Message string                 `json:"message"`
	Result  CommonRenewTokenResult `json:"result"`
}

type RpcClient added in v1.1.0

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

func (*RpcClient) SetHttpHeaderNames added in v1.1.0

func (c *RpcClient) SetHttpHeaderNames(accessCodeHeader, randomKeyHeader string) Client

func (*RpcClient) SetTimeout added in v1.1.0

func (c *RpcClient) SetTimeout(timeout int) Client

func (*RpcClient) SetToken added in v1.1.0

func (c *RpcClient) SetToken(token string) Client

func (*RpcClient) ValidateJwt added in v1.1.0

func (c *RpcClient) ValidateJwt() error

func (*RpcClient) ValidatePermAction added in v1.1.0

func (c *RpcClient) ValidatePermAction(service, path, method string) error

func (*RpcClient) ValidatePermOperationByCode added in v1.1.0

func (c *RpcClient) ValidatePermOperationByCode(operationCode string) error

func (*RpcClient) ValidatePermOrgById added in v1.1.0

func (c *RpcClient) ValidatePermOrgById(orgId string) error

type Validator

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

func NewValidator

func NewValidator(publicKey []byte) *Validator

func (*Validator) ValidateJwt

func (v *Validator) ValidateJwt(tokenString string) (user *JwtUser, err error)

Directories

Path Synopsis
pkg
pb

Jump to

Keyboard shortcuts

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