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"
|
2017-08-16 21:37:37 +00:00
|
|
|
"net/http"
|
2016-04-10 21:39:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
AUTHCODE_EXPIRE_TIME = 60 * 10 // 10 minutes
|
|
|
|
AUTHCODE_RESPONSE_TYPE = "code"
|
2018-11-18 17:55:05 +00:00
|
|
|
IMPLICIT_RESPONSE_TYPE = "token"
|
2016-08-15 16:47:31 +00:00
|
|
|
DEFAULT_SCOPE = "user"
|
2016-04-10 21:39:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type AuthData struct {
|
|
|
|
ClientId string `json:"client_id"`
|
|
|
|
UserId string `json:"user_id"`
|
|
|
|
Code string `json:"code"`
|
|
|
|
ExpiresIn int32 `json:"expires_in"`
|
|
|
|
CreateAt int64 `json:"create_at"`
|
|
|
|
RedirectUri string `json:"redirect_uri"`
|
|
|
|
State string `json:"state"`
|
|
|
|
Scope string `json:"scope"`
|
|
|
|
}
|
|
|
|
|
2017-08-16 21:37:37 +00:00
|
|
|
type AuthorizeRequest struct {
|
|
|
|
ResponseType string `json:"response_type"`
|
|
|
|
ClientId string `json:"client_id"`
|
|
|
|
RedirectUri string `json:"redirect_uri"`
|
|
|
|
Scope string `json:"scope"`
|
|
|
|
State string `json:"state"`
|
|
|
|
}
|
|
|
|
|
2016-04-10 21:39:38 +00:00
|
|
|
// IsValid validates the AuthData and returns an error if it isn't configured
|
|
|
|
// correctly.
|
|
|
|
func (ad *AuthData) IsValid() *AppError {
|
|
|
|
|
2020-08-09 22:29:54 +00:00
|
|
|
if !IsValidId(ad.ClientId) {
|
2018-02-08 23:11:04 +00:00
|
|
|
return NewAppError("AuthData.IsValid", "model.authorize.is_valid.client_id.app_error", nil, "", http.StatusBadRequest)
|
2016-04-10 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
2020-08-09 22:29:54 +00:00
|
|
|
if !IsValidId(ad.UserId) {
|
2018-02-08 23:11:04 +00:00
|
|
|
return NewAppError("AuthData.IsValid", "model.authorize.is_valid.user_id.app_error", nil, "", http.StatusBadRequest)
|
2016-04-10 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(ad.Code) == 0 || len(ad.Code) > 128 {
|
2018-02-08 23:11:04 +00:00
|
|
|
return NewAppError("AuthData.IsValid", "model.authorize.is_valid.auth_code.app_error", nil, "client_id="+ad.ClientId, http.StatusBadRequest)
|
2016-04-10 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ad.ExpiresIn == 0 {
|
2018-02-08 23:11:04 +00:00
|
|
|
return NewAppError("AuthData.IsValid", "model.authorize.is_valid.expires.app_error", nil, "", http.StatusBadRequest)
|
2016-04-10 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ad.CreateAt <= 0 {
|
2018-02-08 23:11:04 +00:00
|
|
|
return NewAppError("AuthData.IsValid", "model.authorize.is_valid.create_at.app_error", nil, "client_id="+ad.ClientId, http.StatusBadRequest)
|
2016-04-10 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
2018-11-18 17:55:05 +00:00
|
|
|
if len(ad.RedirectUri) > 256 || !IsValidHttpUrl(ad.RedirectUri) {
|
2018-02-08 23:11:04 +00:00
|
|
|
return NewAppError("AuthData.IsValid", "model.authorize.is_valid.redirect_uri.app_error", nil, "client_id="+ad.ClientId, http.StatusBadRequest)
|
2016-04-10 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
2018-11-18 17:55:05 +00:00
|
|
|
if len(ad.State) > 1024 {
|
2018-02-08 23:11:04 +00:00
|
|
|
return NewAppError("AuthData.IsValid", "model.authorize.is_valid.state.app_error", nil, "client_id="+ad.ClientId, http.StatusBadRequest)
|
2016-04-10 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(ad.Scope) > 128 {
|
2018-02-08 23:11:04 +00:00
|
|
|
return NewAppError("AuthData.IsValid", "model.authorize.is_valid.scope.app_error", nil, "client_id="+ad.ClientId, http.StatusBadRequest)
|
2016-04-10 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-16 21:37:37 +00:00
|
|
|
// IsValid validates the AuthorizeRequest and returns an error if it isn't configured
|
|
|
|
// correctly.
|
|
|
|
func (ar *AuthorizeRequest) IsValid() *AppError {
|
|
|
|
|
2020-08-09 22:29:54 +00:00
|
|
|
if !IsValidId(ar.ClientId) {
|
2017-08-16 21:37:37 +00:00
|
|
|
return NewAppError("AuthData.IsValid", "model.authorize.is_valid.client_id.app_error", nil, "", http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ar.ResponseType) == 0 {
|
|
|
|
return NewAppError("AuthData.IsValid", "model.authorize.is_valid.response_type.app_error", nil, "", http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ar.RedirectUri) == 0 || len(ar.RedirectUri) > 256 || !IsValidHttpUrl(ar.RedirectUri) {
|
|
|
|
return NewAppError("AuthData.IsValid", "model.authorize.is_valid.redirect_uri.app_error", nil, "client_id="+ar.ClientId, http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
|
2018-11-18 17:55:05 +00:00
|
|
|
if len(ar.State) > 1024 {
|
2017-08-16 21:37:37 +00:00
|
|
|
return NewAppError("AuthData.IsValid", "model.authorize.is_valid.state.app_error", nil, "client_id="+ar.ClientId, http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ar.Scope) > 128 {
|
|
|
|
return NewAppError("AuthData.IsValid", "model.authorize.is_valid.scope.app_error", nil, "client_id="+ar.ClientId, http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-10 21:39:38 +00:00
|
|
|
func (ad *AuthData) PreSave() {
|
|
|
|
if ad.ExpiresIn == 0 {
|
|
|
|
ad.ExpiresIn = AUTHCODE_EXPIRE_TIME
|
|
|
|
}
|
|
|
|
|
|
|
|
if ad.CreateAt == 0 {
|
|
|
|
ad.CreateAt = GetMillis()
|
|
|
|
}
|
2016-08-15 16:47:31 +00:00
|
|
|
|
|
|
|
if len(ad.Scope) == 0 {
|
|
|
|
ad.Scope = DEFAULT_SCOPE
|
|
|
|
}
|
2016-04-10 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ad *AuthData) 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 AuthDataFromJson(data io.Reader) *AuthData {
|
2018-11-18 17:55:05 +00:00
|
|
|
var ad *AuthData
|
|
|
|
json.NewDecoder(data).Decode(&ad)
|
|
|
|
return ad
|
2016-04-10 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
2017-08-16 21:37:37 +00:00
|
|
|
func (ar *AuthorizeRequest) ToJson() string {
|
2018-11-18 17:55:05 +00:00
|
|
|
b, _ := json.Marshal(ar)
|
|
|
|
return string(b)
|
2017-08-16 21:37:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func AuthorizeRequestFromJson(data io.Reader) *AuthorizeRequest {
|
2018-11-18 17:55:05 +00:00
|
|
|
var ar *AuthorizeRequest
|
|
|
|
json.NewDecoder(data).Decode(&ar)
|
|
|
|
return ar
|
2017-08-16 21:37:37 +00:00
|
|
|
}
|
|
|
|
|
2016-04-10 21:39:38 +00:00
|
|
|
func (ad *AuthData) IsExpired() bool {
|
2018-02-08 23:11:04 +00:00
|
|
|
return GetMillis() > ad.CreateAt+int64(ad.ExpiresIn*1000)
|
2016-04-10 21:39:38 +00:00
|
|
|
}
|