github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/model/token.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package model
     5  
     6  import (
     7  	"net/http"
     8  )
     9  
    10  const (
    11  	TOKEN_SIZE            = 64
    12  	MAX_TOKEN_EXIPRY_TIME = 1000 * 60 * 60 * 48 // 48 hour
    13  	TOKEN_TYPE_OAUTH      = "oauth"
    14  )
    15  
    16  type Token struct {
    17  	Token    string
    18  	CreateAt int64
    19  	Type     string
    20  	Extra    string
    21  }
    22  
    23  func NewToken(tokentype, extra string) *Token {
    24  	return &Token{
    25  		Token:    NewRandomString(TOKEN_SIZE),
    26  		CreateAt: GetMillis(),
    27  		Type:     tokentype,
    28  		Extra:    extra,
    29  	}
    30  }
    31  
    32  func (t *Token) IsValid() *AppError {
    33  	if len(t.Token) != TOKEN_SIZE {
    34  		return NewAppError("Token.IsValid", "model.token.is_valid.size", nil, "", http.StatusInternalServerError)
    35  	}
    36  
    37  	if t.CreateAt == 0 {
    38  		return NewAppError("Token.IsValid", "model.token.is_valid.expiry", nil, "", http.StatusInternalServerError)
    39  	}
    40  
    41  	return nil
    42  }