github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/model/access.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  	"encoding/json"
     8  	"io"
     9  	"net/http"
    10  )
    11  
    12  const (
    13  	ACCESS_TOKEN_GRANT_TYPE  = "authorization_code"
    14  	ACCESS_TOKEN_TYPE        = "bearer"
    15  	REFRESH_TOKEN_GRANT_TYPE = "refresh_token"
    16  )
    17  
    18  type AccessData struct {
    19  	ClientId     string `json:"client_id"`
    20  	UserId       string `json:"user_id"`
    21  	Token        string `json:"token"`
    22  	RefreshToken string `json:"refresh_token"`
    23  	RedirectUri  string `json:"redirect_uri"`
    24  	ExpiresAt    int64  `json:"expires_at"`
    25  	Scope        string `json:"scope"`
    26  }
    27  
    28  type AccessResponse struct {
    29  	AccessToken  string `json:"access_token"`
    30  	TokenType    string `json:"token_type"`
    31  	ExpiresIn    int32  `json:"expires_in"`
    32  	Scope        string `json:"scope"`
    33  	RefreshToken string `json:"refresh_token"`
    34  	IdToken      string `json:"id_token"`
    35  }
    36  
    37  // IsValid validates the AccessData and returns an error if it isn't configured
    38  // correctly.
    39  func (ad *AccessData) IsValid() *AppError {
    40  
    41  	if ad.ClientId == "" || len(ad.ClientId) > 26 {
    42  		return NewAppError("AccessData.IsValid", "model.access.is_valid.client_id.app_error", nil, "", http.StatusBadRequest)
    43  	}
    44  
    45  	if ad.UserId == "" || len(ad.UserId) > 26 {
    46  		return NewAppError("AccessData.IsValid", "model.access.is_valid.user_id.app_error", nil, "", http.StatusBadRequest)
    47  	}
    48  
    49  	if len(ad.Token) != 26 {
    50  		return NewAppError("AccessData.IsValid", "model.access.is_valid.access_token.app_error", nil, "", http.StatusBadRequest)
    51  	}
    52  
    53  	if len(ad.RefreshToken) > 26 {
    54  		return NewAppError("AccessData.IsValid", "model.access.is_valid.refresh_token.app_error", nil, "", http.StatusBadRequest)
    55  	}
    56  
    57  	if ad.RedirectUri == "" || len(ad.RedirectUri) > 256 || !IsValidHttpUrl(ad.RedirectUri) {
    58  		return NewAppError("AccessData.IsValid", "model.access.is_valid.redirect_uri.app_error", nil, "", http.StatusBadRequest)
    59  	}
    60  
    61  	return nil
    62  }
    63  
    64  func (ad *AccessData) IsExpired() bool {
    65  
    66  	if ad.ExpiresAt <= 0 {
    67  		return false
    68  	}
    69  
    70  	if GetMillis() > ad.ExpiresAt {
    71  		return true
    72  	}
    73  
    74  	return false
    75  }
    76  
    77  func (ad *AccessData) ToJson() string {
    78  	b, _ := json.Marshal(ad)
    79  	return string(b)
    80  }
    81  
    82  func AccessDataFromJson(data io.Reader) *AccessData {
    83  	var ad *AccessData
    84  	json.NewDecoder(data).Decode(&ad)
    85  	return ad
    86  }
    87  
    88  func (ar *AccessResponse) ToJson() string {
    89  	b, _ := json.Marshal(ar)
    90  	return string(b)
    91  }
    92  
    93  func AccessResponseFromJson(data io.Reader) *AccessResponse {
    94  	var ar *AccessResponse
    95  	json.NewDecoder(data).Decode(&ar)
    96  	return ar
    97  }