github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/api/accesstokens.go (about)

     1  package api
     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.accessTokens.Create(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.accessTokens.List()
    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  }) Response {
    33  	a.accessTokens.Delete(x.ID)
    34  	return NewSuccessResponse(nil)
    35  }
    36  
    37  func (a *API) checkAccessToken(ctx context.Context, x struct {
    38  	ID     string `json:"id"`
    39  	Secret string `json:"secret"`
    40  }) Response {
    41  	if err := a.accessTokens.Check(x.ID, x.Secret); err != nil {
    42  		return NewErrorResponse(err)
    43  	}
    44  
    45  	return NewSuccessResponse(nil)
    46  }