github.com/gigforks/mattermost-server@v4.9.1-0.20180619094218-800d97fa55d0+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 AccessResponseInfo struct { 29 UserName string `json:"username"` 30 } 31 32 type AccessResponse struct { 33 AccessToken string `json:"access_token"` 34 TokenType string `json:"token_type"` 35 ExpiresIn int32 `json:"expires_in"` 36 Scope string `json:"scope"` 37 RefreshToken string `json:"refresh_token"` 38 Info AccessResponseInfo `json:"info"` 39 } 40 41 // IsValid validates the AccessData and returns an error if it isn't configured 42 // correctly. 43 func (ad *AccessData) IsValid() *AppError { 44 45 if len(ad.ClientId) == 0 || len(ad.ClientId) > 26 { 46 return NewAppError("AccessData.IsValid", "model.access.is_valid.client_id.app_error", nil, "", http.StatusBadRequest) 47 } 48 49 if len(ad.UserId) == 0 || len(ad.UserId) > 26 { 50 return NewAppError("AccessData.IsValid", "model.access.is_valid.user_id.app_error", nil, "", http.StatusBadRequest) 51 } 52 53 if len(ad.Token) != 26 { 54 return NewAppError("AccessData.IsValid", "model.access.is_valid.access_token.app_error", nil, "", http.StatusBadRequest) 55 } 56 57 if len(ad.RefreshToken) > 26 { 58 return NewAppError("AccessData.IsValid", "model.access.is_valid.refresh_token.app_error", nil, "", http.StatusBadRequest) 59 } 60 61 if len(ad.RedirectUri) == 0 || len(ad.RedirectUri) > 256 || !IsValidHttpUrl(ad.RedirectUri) { 62 return NewAppError("AccessData.IsValid", "model.access.is_valid.redirect_uri.app_error", nil, "", http.StatusBadRequest) 63 } 64 65 return nil 66 } 67 68 func (me *AccessData) IsExpired() bool { 69 70 if me.ExpiresAt <= 0 { 71 return false 72 } 73 74 if GetMillis() > me.ExpiresAt { 75 return true 76 } 77 78 return false 79 } 80 81 func (ad *AccessData) ToJson() string { 82 b, _ := json.Marshal(ad) 83 return string(b) 84 } 85 86 func AccessDataFromJson(data io.Reader) *AccessData { 87 var ad *AccessData 88 json.NewDecoder(data).Decode(&ad) 89 return ad 90 } 91 92 func (ar *AccessResponse) ToJson() string { 93 b, _ := json.Marshal(ar) 94 return string(b) 95 } 96 97 func AccessResponseFromJson(data io.Reader) *AccessResponse { 98 var ar *AccessResponse 99 json.NewDecoder(data).Decode(&ar) 100 return ar 101 }