goauth

package module
v1.0.21 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

README

goauth

Role, resource and endpoint management service implemented in go. All my go projects integrate with goauth using github.com/curtisnewbie/gocommon.

Requirements

  • MySQL
  • Consul
  • RabbitMQ
  • Redis

Main Concepts

A role is merely a role name with a bunch resources bound to it. A Resource indicates that someone has the permission to access certain endpoints. These endpoints are uniquely identified by a HTTP method and a request URL. goauth is responsible for managing these three concepts. Administrators of a system can create a new role or change the resources that are bound to a role, but the resources and the endpoints are reported by the protected backend services.

Services can report their resources and endpoints to goauth synchronously using goauth's http endpoints or asynchronously using goauth's rabbitmq exchanges. However, since v1.0.20, goauth now prefer to collect resources and endpoints information from backend services using a poll-based mechanism.

Beckend services are expected to declare a REST endpoint GET /auth/resource to expose their paths and resources information. If configured (in goauth), goauth will continually monitor these services by sending HTTP requests to these endpoints in every minute or when the service instance changes (notified by consul).

goauth is designed to work with a gateway service (e.g., gatekeeper) as follows:

Documentation

Index

Constants

View Source
const (
	DefaultMonitorPath = "/auth/resource"
)
View Source
const (
	ErrCodeRoleNotFound = "GA0001"
)
View Source
const (
	ResourceManageResources = "manage-resources"
)

Variables

This section is empty.

Functions

func AddResToRoleIfNotExist

func AddResToRoleIfNotExist(ec miso.Rail, req AddRoleResReq, user common.User) error

func AddResToRoleIfNotExistEp

func AddResToRoleIfNotExistEp(c *gin.Context, ec miso.Rail, req AddRoleResReq) (any, error)

func AddRole

func AddRole(ec miso.Rail, req AddRoleReq, user common.User) error

func AddRoleEp

func AddRoleEp(c *gin.Context, ec miso.Rail, req AddRoleReq) (any, error)

func BindPathRes

func BindPathRes(rail miso.Rail, req BindPathResReq) error

func BindPathResEp

func BindPathResEp(c *gin.Context, ec miso.Rail, req BindPathResReq) (any, error)

func BootstrapServer

func BootstrapServer(args []string)

func CreateMonitoredServiceWatch

func CreateMonitoredServiceWatch(rail miso.Rail, m MonitoredService) error

func CreateMonitoredServiceWatches

func CreateMonitoredServiceWatches(rail miso.Rail) error

func CreatePathIfNotExist

func CreatePathIfNotExist(rail miso.Rail, req CreatePathReq, user common.User) error

func CreateResourceIfNotExist

func CreateResourceIfNotExist(rail miso.Rail, req CreateResReq, user common.User) error

func CreateResourceIfNotExistEp

func CreateResourceIfNotExistEp(c *gin.Context, ec miso.Rail, req CreateResReq) (any, error)

func DeletePath

func DeletePath(ec miso.Rail, req DeletePathReq) error

func DeletePathEp

func DeletePathEp(c *gin.Context, ec miso.Rail, req DeletePathReq) (any, error)

func DeleteResource

func DeleteResource(ec miso.Rail, req DeleteResourceReq) error

func DeleteResourceEp

func DeleteResourceEp(c *gin.Context, ec miso.Rail, req DeleteResourceReq) (any, error)

func GetRoleInfoEp

func GetRoleInfoEp(c *gin.Context, ec miso.Rail, req RoleInfoReq) (any, error)

func ListAllResBriefsEp

func ListAllResBriefsEp(c *gin.Context, ec miso.Rail) (any, error)

func ListAllResBriefsOfRoleEp

func ListAllResBriefsOfRoleEp(c *gin.Context, ec miso.Rail) (any, error)

func ListAllRoleBriefsEp

func ListAllRoleBriefsEp(c *gin.Context, ec miso.Rail) (any, error)

func ListPathsEp

func ListPathsEp(c *gin.Context, ec miso.Rail, req ListPathReq) (any, error)

func ListResourceCandidatesForRoleEp

func ListResourceCandidatesForRoleEp(c *gin.Context, ec miso.Rail) (any, error)

func ListResourcesEp

func ListResourcesEp(c *gin.Context, ec miso.Rail, req ListResReq) (any, error)

func ListRoleResEp

func ListRoleResEp(c *gin.Context, ec miso.Rail, req ListRoleResReq) (any, error)

func ListRolesEp

func ListRolesEp(c *gin.Context, ec miso.Rail, req ListRoleReq) (any, error)

func ListenAddPathEvent

func ListenAddPathEvent(rail miso.Rail, req CreatePathReq) error

func ListenAddResourceEvent

func ListenAddResourceEvent(rail miso.Rail, req CreateResReq) error

func LoadPathResCache

func LoadPathResCache(rail miso.Rail) error

Load cache for path -> resource

func LoadResCodeCache

func LoadResCodeCache(rail miso.Rail) error

Load cache for resource code

func LoadRoleResCache

func LoadRoleResCache(ec miso.Rail) error

Load cache for role -> resources

func QueryResourcePathAsync

func QueryResourcePathAsync(rail miso.Rail, server miso.Server, m MonitoredService)

func RegisterInternalPathResourcesOnBootstrapped

func RegisterInternalPathResourcesOnBootstrapped()

func RegisterWebEndpoints

func RegisterWebEndpoints(rail miso.Rail) error

func RemoveResFromRole

func RemoveResFromRole(ec miso.Rail, req RemoveRoleResReq) error

func RemoveResFromRoleEp

func RemoveResFromRoleEp(c *gin.Context, ec miso.Rail, req RemoveRoleResReq) (any, error)

func ScheduleTasks

func ScheduleTasks(rail miso.Rail) error

func SubEventBus

func SubEventBus(rail miso.Rail) error

func TriggerResourcePathCollection

func TriggerResourcePathCollection(rail miso.Rail, m MonitoredService)

func UnbindPathRes

func UnbindPathRes(ec miso.Rail, req UnbindPathResReq) error

func UnbindPathResEp

func UnbindPathResEp(c *gin.Context, ec miso.Rail, req UnbindPathResReq) (any, error)

func UpdatePath

func UpdatePath(ec miso.Rail, req UpdatePathReq) error

func UpdatePathEp

func UpdatePathEp(c *gin.Context, ec miso.Rail, req UpdatePathReq) (any, error)

Types

type AddRoleReq

type AddRoleReq struct {
	Name string `json:"name" validation:"notEmpty,maxLen:32"` // role name
}

type AddRoleResReq

type AddRoleResReq struct {
	RoleNo  string `json:"roleNo" validation:"notEmpty"`
	ResCode string `json:"resCode" validation:"notEmpty"`
}

type BindPathResReq

type BindPathResReq struct {
	PathNo  string `json:"pathNo" validation:"notEmpty"`
	ResCode string `json:"resCode" validation:"notEmpty"`
}

type CachedUrlRes

type CachedUrlRes struct {
	Id      int      // id
	Pgroup  string   // path group
	PathNo  string   // path no
	ResCode string   // resource code
	Url     string   // url
	Method  string   // http method
	Ptype   PathType // path type: PROTECTED, PUBLIC
}

type CreatePathReq

type CreatePathReq struct {
	Type    PathType `json:"type" validation:"notEmpty"`
	Url     string   `json:"url" validation:"notEmpty,maxLen:128"`
	Group   string   `json:"group" validation:"notEmpty,maxLen:20"`
	Method  string   `json:"method" validation:"notEmpty,maxLen:10"`
	Desc    string   `json:"desc" validation:"maxLen:255"`
	ResCode string   `json:"resCode"`
}

type CreateResReq

type CreateResReq struct {
	Name string `json:"name" validation:"notEmpty,maxLen:32"`
	Code string `json:"code" validation:"notEmpty,maxLen:32"`
}

type DeletePathReq

type DeletePathReq struct {
	PathNo string `json:"pathNo" validation:"notEmpty"`
}

type DeleteResourceReq

type DeleteResourceReq struct {
	ResCode string `json:"resCode" validation:"notEmpty"`
}

type EPath

type EPath struct {
	Id         int      // id
	Pgroup     string   // path group
	PathNo     string   // path no
	Desc       string   // description
	Url        string   // url
	Method     string   // method
	Ptype      PathType // path type: PROTECTED, PUBLIC
	CreateTime miso.ETime
	CreateBy   string
	UpdateTime miso.ETime
	UpdateBy   string
}

type ERes

type ERes struct {
	Id         int    // id
	Code       string // resource code
	Name       string // resource name
	CreateTime miso.ETime
	CreateBy   string
	UpdateTime miso.ETime
	UpdateBy   string
}

type ERole

type ERole struct {
	Id         int
	RoleNo     string
	Name       string
	CreateTime miso.ETime
	CreateBy   string
	UpdateTime miso.ETime
	UpdateBy   string
}

type ERoleRes

type ERoleRes struct {
	Id         int    // id
	RoleNo     string // role no
	ResCode    string // resource code
	CreateTime miso.ETime
	CreateBy   string
	UpdateTime miso.ETime
	UpdateBy   string
}

type ExtendedPathRes

type ExtendedPathRes struct {
	Id         int      // id
	Pgroup     string   // path group
	PathNo     string   // path no
	ResCode    string   // resource code
	Desc       string   // description
	Url        string   // url
	Method     string   // http method
	Ptype      PathType // path type: PROTECTED, PUBLIC
	CreateTime miso.ETime
	CreateBy   string
	UpdateTime miso.ETime
	UpdateBy   string
}

type GenResScriptReq

type GenResScriptReq struct {
	ResCodes []string `json:"resCodes" validation:"notEmpty"`
}

type ListPathReq

type ListPathReq struct {
	ResCode string      `json:"resCode"`
	Pgroup  string      `json:"pgroup"`
	Url     string      `json:"url"`
	Ptype   PathType    `json:"ptype"`
	Paging  miso.Paging `json:"pagingVo"`
}

type ListPathResp

type ListPathResp struct {
	Paging  miso.Paging `json:"pagingVo"`
	Payload []WPath     `json:"payload"`
}

func ListPaths

func ListPaths(ec miso.Rail, req ListPathReq) (ListPathResp, error)

type ListResReq

type ListResReq struct {
	Paging miso.Paging `json:"pagingVo"`
}

type ListResResp

type ListResResp struct {
	Paging  miso.Paging `json:"pagingVo"`
	Payload []WRes      `json:"payload"`
}

func ListResources

func ListResources(ec miso.Rail, req ListResReq) (ListResResp, error)

type ListRoleReq

type ListRoleReq struct {
	Paging miso.Paging `json:"pagingVo"`
}

type ListRoleResReq

type ListRoleResReq struct {
	Paging miso.Paging `json:"pagingVo"`
	RoleNo string      `json:"roleNo" validation:"notEmpty"`
}

type ListRoleResResp

type ListRoleResResp struct {
	Paging  miso.Paging     `json:"pagingVo"`
	Payload []ListedRoleRes `json:"payload"`
}

func ListRoleRes

func ListRoleRes(ec miso.Rail, req ListRoleResReq) (ListRoleResResp, error)

type ListRoleResp

type ListRoleResp struct {
	Payload []WRole     `json:"payload"`
	Paging  miso.Paging `json:"pagingVo"`
}

func ListRoles

func ListRoles(ec miso.Rail, req ListRoleReq) (ListRoleResp, error)

type ListedRoleRes

type ListedRoleRes struct {
	Id         int       `json:"id"`
	ResCode    string    `json:"resCode"`
	ResName    string    `json:"resName"`
	CreateTime time.Time `json:"createTime"`
	CreateBy   string    `json:"createBy"`
}

type MonitorConf

type MonitorConf struct {
	Monitor []MonitoredService
}

type MonitoredService

type MonitoredService struct {
	Service string
	Path    string
	All     bool
}

func LoadMonitoredServices

func LoadMonitoredServices() []MonitoredService

type PathDoc

type PathDoc struct {
	Desc   string
	Type   PathType
	Method string
	Code   string
}

type PathRes

type PathRes struct {
	Id         int    // id
	PathNo     string // path no
	ResCode    string // resource code
	CreateTime miso.ETime
	CreateBy   string
	UpdateTime miso.ETime
	UpdateBy   string
}

type PathType

type PathType string
const (
	// default roleno for admin
	DefaultAdminRoleNo = "role_554107924873216177918"

	PtProtected PathType = "PROTECTED"
	PtPublic    PathType = "PUBLIC"
)

type QueryResourcePathRes

type QueryResourcePathRes struct {
	Resources []CreateResReq
	Paths     []CreatePathReq
}

func QueryResourcePath

func QueryResourcePath(rail miso.Rail, server miso.Server, service string, path string) (QueryResourcePathRes, error)

type RemoveRoleResReq

type RemoveRoleResReq struct {
	RoleNo  string `json:"roleNo" validation:"notEmpty"`
	ResCode string `json:"resCode" validation:"notEmpty"`
}

type ResBrief

type ResBrief struct {
	Code string `json:"code"`
	Name string `json:"name"`
}

func ListAllResBriefs

func ListAllResBriefs(ec miso.Rail) ([]ResBrief, error)

func ListAllResBriefsOfRole

func ListAllResBriefsOfRole(ec miso.Rail, roleNo string) ([]ResBrief, error)

func ListResourceCandidatesForRole

func ListResourceCandidatesForRole(ec miso.Rail, roleNo string) ([]ResBrief, error)

type RoleBrief

type RoleBrief struct {
	RoleNo string `json:"roleNo"`
	Name   string `json:"name"`
}

func ListAllRoleBriefs

func ListAllRoleBriefs(ec miso.Rail) ([]RoleBrief, error)

type RoleInfoReq

type RoleInfoReq struct {
	RoleNo string `json:"roleNo" validation:"notEmpty"`
}

type RoleInfoResp

type RoleInfoResp struct {
	RoleNo string `json:"roleNo"`
	Name   string `json:"name"`
}

func GetRoleInfo

func GetRoleInfo(ec miso.Rail, req RoleInfoReq) (RoleInfoResp, error)

type TestResAccessReq

type TestResAccessReq struct {
	RoleNo string `json:"roleNo"`
	Url    string `json:"url"`
	Method string `json:"method"`
}

type TestResAccessResp

type TestResAccessResp struct {
	Valid bool `json:"valid"`
}

func TestResourceAccess

func TestResourceAccess(ec miso.Rail, req TestResAccessReq) (TestResAccessResp, error)

Test access to resource

type UnbindPathResReq

type UnbindPathResReq struct {
	PathNo  string `json:"pathNo" validation:"notEmpty"`
	ResCode string `json:"resCode" validation:"notEmpty"`
}

type UpdatePathReq

type UpdatePathReq struct {
	Type   PathType `json:"type" validation:"notEmpty"`
	PathNo string   `json:"pathNo" validation:"notEmpty"`
	Group  string   `json:"group" validation:"notEmpty,maxLen:20"`
}

type WPath

type WPath struct {
	Id         int        `json:"id"`
	Pgroup     string     `json:"pgroup"`
	PathNo     string     `json:"pathNo"`
	Method     string     `json:"method"`
	Desc       string     `json:"desc"`
	Url        string     `json:"url"`
	Ptype      PathType   `json:"ptype"`
	CreateTime miso.ETime `json:"createTime"`
	CreateBy   string     `json:"createBy"`
	UpdateTime miso.ETime `json:"updateTime"`
	UpdateBy   string     `json:"updateBy"`
}

type WRes

type WRes struct {
	Id         int        `json:"id"`
	Code       string     `json:"code"`
	Name       string     `json:"name"`
	CreateTime miso.ETime `json:"createTime"`
	CreateBy   string     `json:"createBy"`
	UpdateTime miso.ETime `json:"updateTime"`
	UpdateBy   string     `json:"updateBy"`
}

type WRole

type WRole struct {
	Id         int        `json:"id"`
	RoleNo     string     `json:"roleNo"`
	Name       string     `json:"name"`
	CreateTime miso.ETime `json:"createTime"`
	CreateBy   string     `json:"createBy"`
	UpdateTime miso.ETime `json:"updateTime"`
	UpdateBy   string     `json:"updateBy"`
}

Directories

Path Synopsis
client

Jump to

Keyboard shortcuts

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