github.com/0chain/gosdk@v1.17.11/zboxcore/sdk/authticket.go (about)

     1  package sdk
     2  
     3  import (
     4  	"encoding/base64"
     5  	"encoding/json"
     6  
     7  	"github.com/0chain/errors"
     8  	"github.com/0chain/gosdk/zboxcore/fileref"
     9  	"github.com/0chain/gosdk/zboxcore/marker"
    10  )
    11  
    12  type AuthTicket struct {
    13  	b64Ticket string
    14  }
    15  
    16  // InitAuthTicket initialize auth ticket instance
    17  //   - authTicket: base64 encoded auth ticket
    18  func InitAuthTicket(authTicket string) *AuthTicket {
    19  	at := &AuthTicket{}
    20  	at.b64Ticket = authTicket
    21  	return at
    22  }
    23  
    24  func (at *AuthTicket) IsDir() (bool, error) {
    25  	sEnc, err := base64.StdEncoding.DecodeString(at.b64Ticket)
    26  	if err != nil {
    27  		return false, errors.New("auth_ticket_decode_error", "Error decoding the auth ticket."+err.Error())
    28  	}
    29  	authTicket := &marker.AuthTicket{}
    30  	err = json.Unmarshal(sEnc, authTicket)
    31  	if err != nil {
    32  		return false, errors.New("auth_ticket_decode_error", "Error unmarshaling the auth ticket."+err.Error())
    33  	}
    34  	return authTicket.RefType == fileref.DIRECTORY, nil
    35  }
    36  
    37  func (at *AuthTicket) GetFileName() (string, error) {
    38  	sEnc, err := base64.StdEncoding.DecodeString(at.b64Ticket)
    39  	if err != nil {
    40  		return "", errors.New("auth_ticket_decode_error", "Error decoding the auth ticket."+err.Error())
    41  	}
    42  	authTicket := &marker.AuthTicket{}
    43  	err = json.Unmarshal(sEnc, authTicket)
    44  	if err != nil {
    45  		return "", errors.New("auth_ticket_decode_error", "Error unmarshaling the auth ticket."+err.Error())
    46  	}
    47  	return authTicket.FileName, nil
    48  }
    49  
    50  func (at *AuthTicket) GetLookupHash() (string, error) {
    51  	sEnc, err := base64.StdEncoding.DecodeString(at.b64Ticket)
    52  	if err != nil {
    53  		return "", errors.New("auth_ticket_decode_error", "Error decoding the auth ticket."+err.Error())
    54  	}
    55  	authTicket := &marker.AuthTicket{}
    56  	err = json.Unmarshal(sEnc, authTicket)
    57  	if err != nil {
    58  		return "", errors.New("auth_ticket_decode_error", "Error unmarshaling the auth ticket."+err.Error())
    59  	}
    60  	return authTicket.FilePathHash, nil
    61  }
    62  
    63  func (at *AuthTicket) Unmarshall() (*marker.AuthTicket, error) {
    64  	sEnc, err := base64.StdEncoding.DecodeString(at.b64Ticket)
    65  	if err != nil {
    66  		return nil, errors.New("auth_ticket_decode_error", "Error decoding the auth ticket."+err.Error())
    67  	}
    68  	authTicket := &marker.AuthTicket{}
    69  	err = json.Unmarshal(sEnc, authTicket)
    70  	if err != nil {
    71  		return nil, errors.New("auth_ticket_decode_error", "Error unmarshaling the auth ticket."+err.Error())
    72  	}
    73  	return authTicket, nil
    74  }