github.com/levb/mattermost-server@v5.3.1+incompatible/model/oauth.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 "fmt" 9 "io" 10 "net/http" 11 "unicode/utf8" 12 ) 13 14 const ( 15 OAUTH_ACTION_SIGNUP = "signup" 16 OAUTH_ACTION_LOGIN = "login" 17 OAUTH_ACTION_EMAIL_TO_SSO = "email_to_sso" 18 OAUTH_ACTION_SSO_TO_EMAIL = "sso_to_email" 19 OAUTH_ACTION_MOBILE = "mobile" 20 OAUTH_ACTION_CLIENT = "client" 21 ) 22 23 type OAuthApp struct { 24 Id string `json:"id"` 25 CreatorId string `json:"creator_id"` 26 CreateAt int64 `json:"create_at"` 27 UpdateAt int64 `json:"update_at"` 28 ClientSecret string `json:"client_secret"` 29 Name string `json:"name"` 30 Description string `json:"description"` 31 IconURL string `json:"icon_url"` 32 CallbackUrls StringArray `json:"callback_urls"` 33 Homepage string `json:"homepage"` 34 IsTrusted bool `json:"is_trusted"` 35 } 36 37 // IsValid validates the app and returns an error if it isn't configured 38 // correctly. 39 func (a *OAuthApp) IsValid() *AppError { 40 41 if len(a.Id) != 26 { 42 return NewAppError("OAuthApp.IsValid", "model.oauth.is_valid.app_id.app_error", nil, "", http.StatusBadRequest) 43 } 44 45 if a.CreateAt == 0 { 46 return NewAppError("OAuthApp.IsValid", "model.oauth.is_valid.create_at.app_error", nil, "app_id="+a.Id, http.StatusBadRequest) 47 } 48 49 if a.UpdateAt == 0 { 50 return NewAppError("OAuthApp.IsValid", "model.oauth.is_valid.update_at.app_error", nil, "app_id="+a.Id, http.StatusBadRequest) 51 } 52 53 if len(a.CreatorId) != 26 { 54 return NewAppError("OAuthApp.IsValid", "model.oauth.is_valid.creator_id.app_error", nil, "app_id="+a.Id, http.StatusBadRequest) 55 } 56 57 if len(a.ClientSecret) == 0 || len(a.ClientSecret) > 128 { 58 return NewAppError("OAuthApp.IsValid", "model.oauth.is_valid.client_secret.app_error", nil, "app_id="+a.Id, http.StatusBadRequest) 59 } 60 61 if len(a.Name) == 0 || len(a.Name) > 64 { 62 return NewAppError("OAuthApp.IsValid", "model.oauth.is_valid.name.app_error", nil, "app_id="+a.Id, http.StatusBadRequest) 63 } 64 65 if len(a.CallbackUrls) == 0 || len(fmt.Sprintf("%s", a.CallbackUrls)) > 1024 { 66 return NewAppError("OAuthApp.IsValid", "model.oauth.is_valid.callback.app_error", nil, "app_id="+a.Id, http.StatusBadRequest) 67 } 68 69 for _, callback := range a.CallbackUrls { 70 if !IsValidHttpUrl(callback) { 71 return NewAppError("OAuthApp.IsValid", "model.oauth.is_valid.callback.app_error", nil, "", http.StatusBadRequest) 72 } 73 } 74 75 if len(a.Homepage) == 0 || len(a.Homepage) > 256 || !IsValidHttpUrl(a.Homepage) { 76 return NewAppError("OAuthApp.IsValid", "model.oauth.is_valid.homepage.app_error", nil, "app_id="+a.Id, http.StatusBadRequest) 77 } 78 79 if utf8.RuneCountInString(a.Description) > 512 { 80 return NewAppError("OAuthApp.IsValid", "model.oauth.is_valid.description.app_error", nil, "app_id="+a.Id, http.StatusBadRequest) 81 } 82 83 if len(a.IconURL) > 0 { 84 if len(a.IconURL) > 512 || !IsValidHttpUrl(a.IconURL) { 85 return NewAppError("OAuthApp.IsValid", "model.oauth.is_valid.icon_url.app_error", nil, "app_id="+a.Id, http.StatusBadRequest) 86 } 87 } 88 89 return nil 90 } 91 92 // PreSave will set the Id and ClientSecret if missing. It will also fill 93 // in the CreateAt, UpdateAt times. It should be run before saving the app to the db. 94 func (a *OAuthApp) PreSave() { 95 if a.Id == "" { 96 a.Id = NewId() 97 } 98 99 if a.ClientSecret == "" { 100 a.ClientSecret = NewId() 101 } 102 103 a.CreateAt = GetMillis() 104 a.UpdateAt = a.CreateAt 105 } 106 107 // PreUpdate should be run before updating the app in the db. 108 func (a *OAuthApp) PreUpdate() { 109 a.UpdateAt = GetMillis() 110 } 111 112 func (a *OAuthApp) ToJson() string { 113 b, _ := json.Marshal(a) 114 return string(b) 115 } 116 117 // Generate a valid strong etag so the browser can cache the results 118 func (a *OAuthApp) Etag() string { 119 return Etag(a.Id, a.UpdateAt) 120 } 121 122 // Remove any private data from the app object 123 func (a *OAuthApp) Sanitize() { 124 a.ClientSecret = "" 125 } 126 127 func (a *OAuthApp) IsValidRedirectURL(url string) bool { 128 for _, u := range a.CallbackUrls { 129 if u == url { 130 return true 131 } 132 } 133 134 return false 135 } 136 137 func OAuthAppFromJson(data io.Reader) *OAuthApp { 138 var app *OAuthApp 139 json.NewDecoder(data).Decode(&app) 140 return app 141 } 142 143 func OAuthAppListToJson(l []*OAuthApp) string { 144 b, _ := json.Marshal(l) 145 return string(b) 146 } 147 148 func OAuthAppListFromJson(data io.Reader) []*OAuthApp { 149 var o []*OAuthApp 150 json.NewDecoder(data).Decode(&o) 151 return o 152 }