authorize.go 4.13 KB
Newer Older
1
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
2
3
4
5
6
7
8
// See License.txt for license information.

package model

import (
	"encoding/json"
	"io"
9
	"net/http"
10
11
12
13
14
)

const (
	AUTHCODE_EXPIRE_TIME   = 60 * 10 // 10 minutes
	AUTHCODE_RESPONSE_TYPE = "code"
15
	IMPLICIT_RESPONSE_TYPE = "token"
16
	DEFAULT_SCOPE          = "user"
17
18
19
20
21
22
23
24
25
26
27
28
29
)

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"`
}

30
31
32
33
34
35
36
37
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"`
}

38
39
40
41
42
// IsValid validates the AuthData and returns an error if it isn't configured
// correctly.
func (ad *AuthData) IsValid() *AppError {

	if len(ad.ClientId) != 26 {
43
		return NewAppError("AuthData.IsValid", "model.authorize.is_valid.client_id.app_error", nil, "", http.StatusBadRequest)
44
45
46
	}

	if len(ad.UserId) != 26 {
47
		return NewAppError("AuthData.IsValid", "model.authorize.is_valid.user_id.app_error", nil, "", http.StatusBadRequest)
48
49
50
	}

	if len(ad.Code) == 0 || len(ad.Code) > 128 {
51
		return NewAppError("AuthData.IsValid", "model.authorize.is_valid.auth_code.app_error", nil, "client_id="+ad.ClientId, http.StatusBadRequest)
52
53
54
	}

	if ad.ExpiresIn == 0 {
55
		return NewAppError("AuthData.IsValid", "model.authorize.is_valid.expires.app_error", nil, "", http.StatusBadRequest)
56
57
58
	}

	if ad.CreateAt <= 0 {
59
		return NewAppError("AuthData.IsValid", "model.authorize.is_valid.create_at.app_error", nil, "client_id="+ad.ClientId, http.StatusBadRequest)
60
61
	}

62
	if len(ad.RedirectUri) > 256 || !IsValidHttpUrl(ad.RedirectUri) {
63
		return NewAppError("AuthData.IsValid", "model.authorize.is_valid.redirect_uri.app_error", nil, "client_id="+ad.ClientId, http.StatusBadRequest)
64
65
	}

66
	if len(ad.State) > 1024 {
67
		return NewAppError("AuthData.IsValid", "model.authorize.is_valid.state.app_error", nil, "client_id="+ad.ClientId, http.StatusBadRequest)
68
69
70
	}

	if len(ad.Scope) > 128 {
71
		return NewAppError("AuthData.IsValid", "model.authorize.is_valid.scope.app_error", nil, "client_id="+ad.ClientId, http.StatusBadRequest)
72
73
74
75
76
	}

	return nil
}

77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// IsValid validates the AuthorizeRequest and returns an error if it isn't configured
// correctly.
func (ar *AuthorizeRequest) IsValid() *AppError {

	if len(ar.ClientId) != 26 {
		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)
	}

93
	if len(ar.State) > 1024 {
94
95
96
97
98
99
100
101
102
103
		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
}

104
105
106
107
108
109
110
111
func (ad *AuthData) PreSave() {
	if ad.ExpiresIn == 0 {
		ad.ExpiresIn = AUTHCODE_EXPIRE_TIME
	}

	if ad.CreateAt == 0 {
		ad.CreateAt = GetMillis()
	}
112
113
114
115

	if len(ad.Scope) == 0 {
		ad.Scope = DEFAULT_SCOPE
	}
116
117
118
}

func (ad *AuthData) ToJson() string {
Chris's avatar
Chris committed
119
120
	b, _ := json.Marshal(ad)
	return string(b)
121
122
123
}

func AuthDataFromJson(data io.Reader) *AuthData {
Chris's avatar
Chris committed
124
125
126
	var ad *AuthData
	json.NewDecoder(data).Decode(&ad)
	return ad
127
128
}

129
func (ar *AuthorizeRequest) ToJson() string {
Chris's avatar
Chris committed
130
131
	b, _ := json.Marshal(ar)
	return string(b)
132
133
134
}

func AuthorizeRequestFromJson(data io.Reader) *AuthorizeRequest {
Chris's avatar
Chris committed
135
136
137
	var ar *AuthorizeRequest
	json.NewDecoder(data).Decode(&ar)
	return ar
138
139
}

140
func (ad *AuthData) IsExpired() bool {
Chris's avatar
Chris committed
141
	return GetMillis() > ad.CreateAt+int64(ad.ExpiresIn*1000)
142
}