2017-08-16 21:37:37 +00:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
2020-08-09 22:29:54 +00:00
|
|
|
// See LICENSE.txt for license information.
|
2016-04-10 21:39:38 +00:00
|
|
|
|
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
2018-02-08 23:11:04 +00:00
|
|
|
"net/http"
|
2016-04-10 21:39:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ACCESS_TOKEN_GRANT_TYPE = "authorization_code"
|
|
|
|
ACCESS_TOKEN_TYPE = "bearer"
|
|
|
|
REFRESH_TOKEN_GRANT_TYPE = "refresh_token"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AccessData struct {
|
2016-08-15 16:47:31 +00:00
|
|
|
ClientId string `json:"client_id"`
|
|
|
|
UserId string `json:"user_id"`
|
2016-04-10 21:39:38 +00:00
|
|
|
Token string `json:"token"`
|
|
|
|
RefreshToken string `json:"refresh_token"`
|
|
|
|
RedirectUri string `json:"redirect_uri"`
|
2016-08-15 16:47:31 +00:00
|
|
|
ExpiresAt int64 `json:"expires_at"`
|
2017-08-16 21:37:37 +00:00
|
|
|
Scope string `json:"scope"`
|
2016-04-10 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type AccessResponse struct {
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
TokenType string `json:"token_type"`
|
|
|
|
ExpiresIn int32 `json:"expires_in"`
|
|
|
|
Scope string `json:"scope"`
|
|
|
|
RefreshToken string `json:"refresh_token"`
|
2021-10-16 22:47:22 +00:00
|
|
|
IdToken string `json:"id_token"`
|
2016-04-10 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsValid validates the AccessData and returns an error if it isn't configured
|
|
|
|
// correctly.
|
|
|
|
func (ad *AccessData) IsValid() *AppError {
|
|
|
|
|
2021-10-16 22:47:22 +00:00
|
|
|
if ad.ClientId == "" || len(ad.ClientId) > 26 {
|
2018-02-08 23:11:04 +00:00
|
|
|
return NewAppError("AccessData.IsValid", "model.access.is_valid.client_id.app_error", nil, "", http.StatusBadRequest)
|
2016-08-15 16:47:31 +00:00
|
|
|
}
|
|
|
|
|
2021-10-16 22:47:22 +00:00
|
|
|
if ad.UserId == "" || len(ad.UserId) > 26 {
|
2018-02-08 23:11:04 +00:00
|
|
|
return NewAppError("AccessData.IsValid", "model.access.is_valid.user_id.app_error", nil, "", http.StatusBadRequest)
|
2016-04-10 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(ad.Token) != 26 {
|
2018-02-08 23:11:04 +00:00
|
|
|
return NewAppError("AccessData.IsValid", "model.access.is_valid.access_token.app_error", nil, "", http.StatusBadRequest)
|
2016-04-10 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(ad.RefreshToken) > 26 {
|
2018-02-08 23:11:04 +00:00
|
|
|
return NewAppError("AccessData.IsValid", "model.access.is_valid.refresh_token.app_error", nil, "", http.StatusBadRequest)
|
2016-04-10 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
2021-10-16 22:47:22 +00:00
|
|
|
if ad.RedirectUri == "" || len(ad.RedirectUri) > 256 || !IsValidHttpUrl(ad.RedirectUri) {
|
2018-02-08 23:11:04 +00:00
|
|
|
return NewAppError("AccessData.IsValid", "model.access.is_valid.redirect_uri.app_error", nil, "", http.StatusBadRequest)
|
2016-04-10 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-09 22:29:54 +00:00
|
|
|
func (ad *AccessData) IsExpired() bool {
|
2016-08-15 16:47:31 +00:00
|
|
|
|
2020-08-09 22:29:54 +00:00
|
|
|
if ad.ExpiresAt <= 0 {
|
2016-08-15 16:47:31 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-08-09 22:29:54 +00:00
|
|
|
if GetMillis() > ad.ExpiresAt {
|
2016-08-15 16:47:31 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-04-10 21:39:38 +00:00
|
|
|
func (ad *AccessData) ToJson() string {
|
2018-11-18 17:55:05 +00:00
|
|
|
b, _ := json.Marshal(ad)
|
|
|
|
return string(b)
|
2016-04-10 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func AccessDataFromJson(data io.Reader) *AccessData {
|
2018-11-18 17:55:05 +00:00
|
|
|
var ad *AccessData
|
|
|
|
json.NewDecoder(data).Decode(&ad)
|
|
|
|
return ad
|
2016-04-10 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ar *AccessResponse) ToJson() string {
|
2018-11-18 17:55:05 +00:00
|
|
|
b, _ := json.Marshal(ar)
|
|
|
|
return string(b)
|
2016-04-10 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func AccessResponseFromJson(data io.Reader) *AccessResponse {
|
2018-11-18 17:55:05 +00:00
|
|
|
var ar *AccessResponse
|
|
|
|
json.NewDecoder(data).Decode(&ar)
|
|
|
|
return ar
|
2016-04-10 21:39:38 +00:00
|
|
|
}
|