109 lines
2.6 KiB
Go
109 lines
2.6 KiB
Go
|
package server
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
|
||
|
"github.com/Xe/ln"
|
||
|
"github.com/Xe/uuid"
|
||
|
|
||
|
proto "git.xeserv.us/xena/route/proto"
|
||
|
"google.golang.org/grpc/codes"
|
||
|
"google.golang.org/grpc/status"
|
||
|
)
|
||
|
|
||
|
// Token is the token server implementation for grpc use.
|
||
|
type Token struct {
|
||
|
*Server
|
||
|
}
|
||
|
|
||
|
// interface assertions
|
||
|
var (
|
||
|
_ proto.TokensServer = &Token{}
|
||
|
)
|
||
|
|
||
|
func (t *Token) Get(ctx context.Context, req *proto.GetTokenRequest) (*proto.Token, error) {
|
||
|
clitok, err := t.getAuth(ctx, "token:get")
|
||
|
if err != nil {
|
||
|
return nil, handleError(ctx, clitok, err, ln.F{"action": "Token.Get_getAuth"})
|
||
|
}
|
||
|
|
||
|
if req.Id == "" {
|
||
|
return nil, status.Errorf(codes.InvalidArgument, "must specify ID")
|
||
|
}
|
||
|
|
||
|
dbt, err := t.db.GetTokenID(ctx, req.Id)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
if dbt.Owner != clitok.Owner {
|
||
|
return nil, ErrNotAuthorized
|
||
|
}
|
||
|
|
||
|
return dbt.AsProto(), nil
|
||
|
}
|
||
|
|
||
|
func (t *Token) GetAll(ctx context.Context, req *proto.Nil) (*proto.TokenSet, error) {
|
||
|
clitok, err := t.getAuth(ctx, "token:getall")
|
||
|
if err != nil {
|
||
|
return nil, handleError(ctx, clitok, err, ln.F{"action": "Token.GetAll_getAuth"})
|
||
|
}
|
||
|
|
||
|
toks, err := t.db.GetTokensForOwner(ctx, clitok.Owner)
|
||
|
if err != nil {
|
||
|
return nil, handleError(ctx, clitok, err, ln.F{"action": "Token.GetAll_db.GetTokensForOwner"})
|
||
|
}
|
||
|
|
||
|
result := []*proto.Token{}
|
||
|
|
||
|
for _, tok := range toks {
|
||
|
result = append(result, tok.AsProto())
|
||
|
}
|
||
|
|
||
|
return &proto.TokenSet{
|
||
|
Tokens: result,
|
||
|
}, nil
|
||
|
}
|
||
|
|
||
|
func (t *Token) Put(ctx context.Context, tok *proto.Token) (*proto.Token, error) {
|
||
|
clitok, err := t.getAuth(ctx, "token:put")
|
||
|
if err != nil {
|
||
|
return nil, handleError(ctx, clitok, err, ln.F{"action": "Token.Put_getAuth"})
|
||
|
}
|
||
|
|
||
|
dbt, err := t.db.PutToken(ctx, uuid.New(), clitok.Owner, tok.Scopes)
|
||
|
if err != nil {
|
||
|
return nil, handleError(ctx, clitok, err, ln.F{"action": "Token.Put_db.PutToken"})
|
||
|
}
|
||
|
|
||
|
return dbt.AsProto(), nil
|
||
|
}
|
||
|
|
||
|
func (t *Token) Delete(ctx context.Context, tok *proto.Token) (*proto.Nil, error) {
|
||
|
clitok, err := t.getAuth(ctx, "token:delete")
|
||
|
if err != nil {
|
||
|
return nil, handleError(ctx, clitok, err, ln.F{"action": "Token.Delete_getAuth"})
|
||
|
}
|
||
|
|
||
|
err = t.db.DeleteToken(ctx, tok.Id)
|
||
|
if err != nil {
|
||
|
return nil, handleError(ctx, clitok, err, ln.F{"action": "Token.Delete_db.DeleteToken"})
|
||
|
}
|
||
|
|
||
|
return &proto.Nil{}, nil
|
||
|
}
|
||
|
|
||
|
func (t *Token) Deactivate(ctx context.Context, tok *proto.Token) (*proto.Nil, error) {
|
||
|
clitok, err := t.getAuth(ctx, "token:deactivate")
|
||
|
if err != nil {
|
||
|
return nil, handleError(ctx, clitok, err, ln.F{"action": "Token.Deactivate_getAuth"})
|
||
|
}
|
||
|
|
||
|
err = t.db.DeactivateToken(ctx, tok.Id)
|
||
|
if err != nil {
|
||
|
return nil, handleError(ctx, clitok, err, ln.F{"action": "Token.Deactivate_db.DeactivateToken"})
|
||
|
}
|
||
|
|
||
|
return &proto.Nil{}, nil
|
||
|
}
|