github.com/cjdelisle/matterfoss@v5.11.1+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, _ := json.Marshal(ad) 78 return string(b) 79 } 80 81 func AccessDataFromJson(data io.Reader) *AccessData { 82 var ad *AccessData 83 json.NewDecoder(data).Decode(&ad) 84 return ad 85 } 86 87 func (ar *AccessResponse) ToJson() string { 88 b, _ := json.Marshal(ar) 89 return string(b) 90 } 91 92 func AccessResponseFromJson(data io.Reader) *AccessResponse { 93 var ar *AccessResponse 94 json.NewDecoder(data).Decode(&ar) 95 return ar 96 }