github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/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  	//TODO Add delete permission verify.
    34  	if err := a.accessTokens.Delete(x.ID); err != nil {
    35  		return NewErrorResponse(err)
    36  	}
    37  	return NewSuccessResponse(nil)
    38  }
    39  
    40  func (a *API) checkAccessToken(ctx context.Context, x struct {
    41  	ID     string `json:"id"`
    42  	Secret string `json:"secret"`
    43  }) Response {
    44  	if err := a.accessTokens.Check(x.ID, x.Secret); err != nil {
    45  		return NewErrorResponse(err)
    46  	}
    47  
    48  	return NewSuccessResponse(nil)
    49  }