token

package
v0.0.0-...-8c4fe33 Latest Latest
Warning

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

Go to latest
Published: May 29, 2023 License: MIT Imports: 12 Imported by: 0

README

Token 认证模块

首先说明,为什么要单独拿出来做一个模块:

  • 服务大起来后,都需要来这里获取Token,而需要Token的可能有
    • 内部服务用户、SDK用户、HTTP用户...
  • 而且可以有很多种Token的机制:
    • 用户名密码、Access_Token...

所以考虑之后的扩展性,将其单独做成了一个模块。

这里会采用 mongodb + redis 的认证方式

安装mongodb

  • 使用docker简单安装,docker自行安装
docker pull mongo
  • 运行并进入客户端
docker run -itd -p 27017:27017 mongo
  • 添加管理员
use admin # 使用数据库
db.createUser({user:"admin",pwd:"root",roles:["root"]}) # 创建认证用户
db.auth("admin", "root") # 验证用户
  • 添加认证数据库用户
use apirooter
db.createUser({user: "apirooter", pwd: "123456", roles: [{ role: "dbOwner", db: "apirooter" }]})
db.auth("apirooter", "123456")

Documentation

Overview

@Author: Ciusyan 2023/2/6

Index

Constants

View Source
const (
	AppName = "token"
)

Variables

View Source
var File_apps_token_pb_token_proto protoreflect.FileDescriptor
View Source
var Service_ServiceDesc = grpc.ServiceDesc{
	ServiceName: "dousheng.token.Service",
	HandlerType: (*ServiceServer)(nil),
	Methods: []grpc.MethodDesc{
		{
			MethodName: "IssueToken",
			Handler:    _Service_IssueToken_Handler,
		},
		{
			MethodName: "ValidateToken",
			Handler:    _Service_ValidateToken_Handler,
		},
		{
			MethodName: "GetUIDFromTk",
			Handler:    _Service_GetUIDFromTk_Handler,
		},
	},
	Streams:  []grpc.StreamDesc{},
	Metadata: "apps/token/pb/token.proto",
}

Service_ServiceDesc is the grpc.ServiceDesc for Service service. It's only intended for direct use with grpc.RegisterService, and not to be introspected or modified (even as a copy)

Functions

func RegisterServiceServer

func RegisterServiceServer(s grpc.ServiceRegistrar, srv ServiceServer)

Types

type IssueTokenRequest

type IssueTokenRequest struct {

	// 颁发人名称
	// @gotags: json:"username" bson:"username"
	Username string `protobuf:"bytes,1,opt,name=username,proto3" json:"username" bson:"username"`
	// 颁发人ID
	// @gotags: json:"user_id" bson:"user_id"
	UserId int64 `protobuf:"varint,2,opt,name=user_id,json=userId,proto3" json:"user_id" bson:"user_id"`
	// contains filtered or unexported fields
}

func (*IssueTokenRequest) Descriptor deprecated

func (*IssueTokenRequest) Descriptor() ([]byte, []int)

Deprecated: Use IssueTokenRequest.ProtoReflect.Descriptor instead.

func (*IssueTokenRequest) GetUserId

func (x *IssueTokenRequest) GetUserId() int64

func (*IssueTokenRequest) GetUsername

func (x *IssueTokenRequest) GetUsername() string

func (*IssueTokenRequest) ProtoMessage

func (*IssueTokenRequest) ProtoMessage()

func (*IssueTokenRequest) ProtoReflect

func (x *IssueTokenRequest) ProtoReflect() protoreflect.Message

func (*IssueTokenRequest) Reset

func (x *IssueTokenRequest) Reset()

func (*IssueTokenRequest) String

func (x *IssueTokenRequest) String() string

type ServiceClient

type ServiceClient interface {
	// 颁发 Token
	IssueToken(ctx context.Context, in *IssueTokenRequest, opts ...grpc.CallOption) (*Token, error)
	// 验证Token (内部服务使用)
	ValidateToken(ctx context.Context, in *ValidateTokenRequest, opts ...grpc.CallOption) (*Token, error)
	// 根据Token解析出 user_id
	GetUIDFromTk(ctx context.Context, in *ValidateTokenRequest, opts ...grpc.CallOption) (*UIDResponse, error)
}

ServiceClient is the client API for Service service.

For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.

func NewServiceClient

func NewServiceClient(cc grpc.ClientConnInterface) ServiceClient

type ServiceServer

type ServiceServer interface {
	// 颁发 Token
	IssueToken(context.Context, *IssueTokenRequest) (*Token, error)
	// 验证Token (内部服务使用)
	ValidateToken(context.Context, *ValidateTokenRequest) (*Token, error)
	// 根据Token解析出 user_id
	GetUIDFromTk(context.Context, *ValidateTokenRequest) (*UIDResponse, error)
	// contains filtered or unexported methods
}

ServiceServer is the server API for Service service. All implementations must embed UnimplementedServiceServer for forward compatibility

type Token

type Token struct {

	// 唯一ID
	// @gotags: json:"access_token" bson:"_id"
	AccessToken string `protobuf:"bytes,1,opt,name=access_token,json=accessToken,proto3" json:"access_token" bson:"_id"`
	// 颁发时间
	// @gotags: json:"issue_at" bson:"issue_at"
	IssueAt int64 `protobuf:"varint,2,opt,name=issue_at,json=issueAt,proto3" json:"issue_at" bson:"issue_at"`
	// 颁发人
	// @gotags: json:"issue_by" bson:"issue_by"
	IssueBy string `protobuf:"bytes,3,opt,name=issue_by,json=issueBy,proto3" json:"issue_by" bson:"issue_by"`
	// 更新时间
	// @gotags: json:"update_at" bson:"update_at"
	UpdateAt int64 `protobuf:"varint,4,opt,name=update_at,json=updateAt,proto3" json:"update_at" bson:"update_at"`
	// 更新人
	// @gotags: json:"update_by" bson:"update_by"
	UpdateBy string `protobuf:"bytes,5,opt,name=update_by,json=updateBy,proto3" json:"update_by" bson:"update_by"`
	// Access Token过期时间(绝对时间), 10分钟,  now() + 10分钟
	// @gotags: json:"access_token_expired_at" bson:"access_token_expired_at"
	AccessTokenExpiredAt int64 `` /* 147-byte string literal not displayed */
	// token过期了, 允许刷新
	// @gotags: json:"refresh_token" bson:"refresh_token"
	RefreshToken string `protobuf:"bytes,7,opt,name=refresh_token,json=refreshToken,proto3" json:"refresh_token" bson:"refresh_token"`
	// Access Token过期时间(绝对时间), 10分钟,  now() + 10分钟
	// @gotags: json:"refresh_token_expired_at" bson:"refresh_token_expired_at"
	RefreshTokenExpiredAt int64 `` /* 151-byte string literal not displayed */
	// 用于传递额外信息
	// @gotags: json:"meta" bson:"meta"
	Meta map[string]string `` /* 152-byte string literal not displayed */
	// contains filtered or unexported fields
}

Token

func NewDefaultToken

func NewDefaultToken() *Token

func NewToken

func NewToken(req *IssueTokenRequest, expiredDuration time.Duration) *Token

func (*Token) Descriptor deprecated

func (*Token) Descriptor() ([]byte, []int)

Deprecated: Use Token.ProtoReflect.Descriptor instead.

func (*Token) Extend

func (t *Token) Extend(expiredDuration time.Duration) *Token

Extend 续约Token

func (*Token) GetAccessToken

func (x *Token) GetAccessToken() string

func (*Token) GetAccessTokenExpiredAt

func (x *Token) GetAccessTokenExpiredAt() int64

func (*Token) GetIssueAt

func (x *Token) GetIssueAt() int64

func (*Token) GetIssueBy

func (x *Token) GetIssueBy() string

func (*Token) GetMeta

func (x *Token) GetMeta() map[string]string

func (*Token) GetRefreshToken

func (x *Token) GetRefreshToken() string

func (*Token) GetRefreshTokenExpiredAt

func (x *Token) GetRefreshTokenExpiredAt() int64

func (*Token) GetUpdateAt

func (x *Token) GetUpdateAt() int64

func (*Token) GetUpdateBy

func (x *Token) GetUpdateBy() string

func (*Token) GetUserId

func (t *Token) GetUserId() int64

GetUserId 从Meta中获取 用户ID

func (*Token) IsExpired

func (s *Token) IsExpired(expiredAt int64) bool

IsExpired 判断AccessToken、RefreshToken有没有过期

func (*Token) ProtoMessage

func (*Token) ProtoMessage()

func (*Token) ProtoReflect

func (x *Token) ProtoReflect() protoreflect.Message

func (*Token) Reset

func (x *Token) Reset()

func (*Token) String

func (x *Token) String() string

type UIDResponse

type UIDResponse struct {

	// access token
	// @gotags: json:"user_id"
	UserId int64 `protobuf:"varint,1,opt,name=user_id,json=userId,proto3" json:"user_id"`
	// contains filtered or unexported fields
}

func NewUIDResponse

func NewUIDResponse() *UIDResponse

func (*UIDResponse) Descriptor deprecated

func (*UIDResponse) Descriptor() ([]byte, []int)

Deprecated: Use UIDResponse.ProtoReflect.Descriptor instead.

func (*UIDResponse) GetUserId

func (x *UIDResponse) GetUserId() int64

func (*UIDResponse) ProtoMessage

func (*UIDResponse) ProtoMessage()

func (*UIDResponse) ProtoReflect

func (x *UIDResponse) ProtoReflect() protoreflect.Message

func (*UIDResponse) Reset

func (x *UIDResponse) Reset()

func (*UIDResponse) String

func (x *UIDResponse) String() string

type UnimplementedServiceServer

type UnimplementedServiceServer struct {
}

UnimplementedServiceServer must be embedded to have forward compatible implementations.

func (UnimplementedServiceServer) GetUIDFromTk

func (UnimplementedServiceServer) IssueToken

func (UnimplementedServiceServer) ValidateToken

type UnsafeServiceServer

type UnsafeServiceServer interface {
	// contains filtered or unexported methods
}

UnsafeServiceServer may be embedded to opt out of forward compatibility for this service. Use of this interface is not recommended, as added methods to ServiceServer will result in compilation errors.

type ValidateTokenRequest

type ValidateTokenRequest struct {

	// access token
	// @gotags: json:"access_token"
	AccessToken string `protobuf:"bytes,1,opt,name=access_token,json=accessToken,proto3" json:"access_token"`
	// contains filtered or unexported fields
}

func NewValidateTokenRequest

func NewValidateTokenRequest(ak string) *ValidateTokenRequest

func (*ValidateTokenRequest) Descriptor deprecated

func (*ValidateTokenRequest) Descriptor() ([]byte, []int)

Deprecated: Use ValidateTokenRequest.ProtoReflect.Descriptor instead.

func (*ValidateTokenRequest) GetAccessToken

func (x *ValidateTokenRequest) GetAccessToken() string

func (*ValidateTokenRequest) ProtoMessage

func (*ValidateTokenRequest) ProtoMessage()

func (*ValidateTokenRequest) ProtoReflect

func (x *ValidateTokenRequest) ProtoReflect() protoreflect.Message

func (*ValidateTokenRequest) Reset

func (x *ValidateTokenRequest) Reset()

func (*ValidateTokenRequest) String

func (x *ValidateTokenRequest) String() string

Directories

Path Synopsis
@Author: Ciusyan 2023/2/6
@Author: Ciusyan 2023/2/6

Jump to

Keyboard shortcuts

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