code.vegaprotocol.io/vega@v0.79.0/wallet/service/v2/connections/token.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package connections
    17  
    18  import (
    19  	"time"
    20  
    21  	vgrand "code.vegaprotocol.io/vega/libs/rand"
    22  )
    23  
    24  type Token string
    25  
    26  func (t Token) String() string {
    27  	return string(t)
    28  }
    29  
    30  func (t Token) Short() string {
    31  	if len(t) > 0 {
    32  		return string([]byte(t)[:4]) + ".." + string([]byte(t)[len(t)-5:])
    33  	}
    34  	return ""
    35  }
    36  
    37  func GenerateToken() Token {
    38  	return Token(vgrand.RandomStr(64))
    39  }
    40  
    41  func AsToken(token string) (Token, error) {
    42  	if len(token) == 0 {
    43  		return "", ErrTokenIsRequired
    44  	}
    45  	if len(token) != 64 {
    46  		return "", ErrInvalidTokenFormat
    47  	}
    48  	return Token(token), nil
    49  }
    50  
    51  type TokenSummary struct {
    52  	Description    string     `json:"description"`
    53  	Token          Token      `json:"token"`
    54  	CreationDate   time.Time  `json:"creationDate"`
    55  	ExpirationDate *time.Time `json:"expirationDate"`
    56  }
    57  
    58  type WalletCredentials struct {
    59  	Name       string `json:"name"`
    60  	Passphrase string `json:"passphrase"`
    61  }
    62  
    63  type Session struct {
    64  	Token    Token  `json:"token"`
    65  	Hostname string `json:"hostname"`
    66  	Wallet   string `json:"wallet"`
    67  }