github.com/psyb0t/mattermost-server@v4.6.1-0.20180125161845-5503a1351abf+incompatible/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  }
    35  
    36  // IsValid validates the AccessData and returns an error if it isn't configured
    37  // correctly.
    38  func (ad *AccessData) IsValid() *AppError {
    39  
    40  	if len(ad.ClientId) == 0 || len(ad.ClientId) > 26 {
    41  		return NewAppError("AccessData.IsValid", "model.access.is_valid.client_id.app_error", nil, "", http.StatusBadRequest)
    42  	}
    43  
    44  	if len(ad.UserId) == 0 || len(ad.UserId) > 26 {
    45  		return NewAppError("AccessData.IsValid", "model.access.is_valid.user_id.app_error", nil, "", http.StatusBadRequest)
    46  	}
    47  
    48  	if len(ad.Token) != 26 {
    49  		return NewAppError("AccessData.IsValid", "model.access.is_valid.access_token.app_error", nil, "", http.StatusBadRequest)
    50  	}
    51  
    52  	if len(ad.RefreshToken) > 26 {
    53  		return NewAppError("AccessData.IsValid", "model.access.is_valid.refresh_token.app_error", nil, "", http.StatusBadRequest)
    54  	}
    55  
    56  	if len(ad.RedirectUri) == 0 || len(ad.RedirectUri) > 256 || !IsValidHttpUrl(ad.RedirectUri) {
    57  		return NewAppError("AccessData.IsValid", "model.access.is_valid.redirect_uri.app_error", nil, "", http.StatusBadRequest)
    58  	}
    59  
    60  	return nil
    61  }
    62  
    63  func (me *AccessData) IsExpired() bool {
    64  
    65  	if me.ExpiresAt <= 0 {
    66  		return false
    67  	}
    68  
    69  	if GetMillis() > me.ExpiresAt {
    70  		return true
    71  	}
    72  
    73  	return false
    74  }
    75  
    76  func (ad *AccessData) ToJson() string {
    77  	b, err := json.Marshal(ad)
    78  	if err != nil {
    79  		return ""
    80  	} else {
    81  		return string(b)
    82  	}
    83  }
    84  
    85  func AccessDataFromJson(data io.Reader) *AccessData {
    86  	decoder := json.NewDecoder(data)
    87  	var ad AccessData
    88  	err := decoder.Decode(&ad)
    89  	if err == nil {
    90  		return &ad
    91  	} else {
    92  		return nil
    93  	}
    94  }
    95  
    96  func (ar *AccessResponse) ToJson() string {
    97  	b, err := json.Marshal(ar)
    98  	if err != nil {
    99  		return ""
   100  	} else {
   101  		return string(b)
   102  	}
   103  }
   104  
   105  func AccessResponseFromJson(data io.Reader) *AccessResponse {
   106  	decoder := json.NewDecoder(data)
   107  	var ar AccessResponse
   108  	err := decoder.Decode(&ar)
   109  	if err == nil {
   110  		return &ar
   111  	} else {
   112  		return nil
   113  	}
   114  }