OSDN Git Service

make some fields of BlockchainReactor public to facilitate separate API (#477)
[bytom/bytom.git] / blockchain / accesstokens.go
1 package blockchain
2
3 import (
4         "context"
5
6         log "github.com/sirupsen/logrus"
7 )
8
9 func (a *API) createAccessToken(ctx context.Context, x struct {
10         ID   string `json:"id"`
11         Type string `json:"type"`
12 }) Response {
13         token, err := a.wallet.Tokens.Create(ctx, x.ID, x.Type)
14         if err != nil {
15                 return NewErrorResponse(err)
16         }
17         return NewSuccessResponse(token)
18 }
19
20 func (a *API) listAccessTokens(ctx context.Context) Response {
21         tokens, err := a.wallet.Tokens.List(ctx)
22         if err != nil {
23                 log.Errorf("listAccessTokens: %v", err)
24                 return NewErrorResponse(err)
25         }
26
27         return NewSuccessResponse(tokens)
28 }
29
30 func (a *API) deleteAccessToken(ctx context.Context, x struct {
31         ID    string `json:"id"`
32         Token string `json:"token"`
33 }) Response {
34         //TODO Add delete permission verify.
35         if err := a.wallet.Tokens.Delete(ctx, x.ID); err != nil {
36                 return NewErrorResponse(err)
37         }
38         return NewSuccessResponse(nil)
39 }
40
41 func (a *API) checkAccessToken(ctx context.Context, x struct {
42         ID     string `json:"id"`
43         Secret string `json:"secret"`
44 }) Response {
45         if _, err := a.wallet.Tokens.Check(ctx, x.ID, x.Secret); err != nil {
46                 return NewErrorResponse(err)
47         }
48
49         return NewSuccessResponse(nil)
50 }