github.com/mad-app/mattermost-server@v5.11.1+incompatible/store/sqlstore/oauth_store.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package sqlstore 5 6 import ( 7 "net/http" 8 "strings" 9 10 "github.com/mattermost/gorp" 11 "github.com/mattermost/mattermost-server/model" 12 "github.com/mattermost/mattermost-server/store" 13 ) 14 15 type SqlOAuthStore struct { 16 SqlStore 17 } 18 19 func NewSqlOAuthStore(sqlStore SqlStore) store.OAuthStore { 20 as := &SqlOAuthStore{sqlStore} 21 22 for _, db := range sqlStore.GetAllConns() { 23 table := db.AddTableWithName(model.OAuthApp{}, "OAuthApps").SetKeys(false, "Id") 24 table.ColMap("Id").SetMaxSize(26) 25 table.ColMap("CreatorId").SetMaxSize(26) 26 table.ColMap("ClientSecret").SetMaxSize(128) 27 table.ColMap("Name").SetMaxSize(64) 28 table.ColMap("Description").SetMaxSize(512) 29 table.ColMap("CallbackUrls").SetMaxSize(1024) 30 table.ColMap("Homepage").SetMaxSize(256) 31 table.ColMap("IconURL").SetMaxSize(512) 32 33 tableAuth := db.AddTableWithName(model.AuthData{}, "OAuthAuthData").SetKeys(false, "Code") 34 tableAuth.ColMap("UserId").SetMaxSize(26) 35 tableAuth.ColMap("ClientId").SetMaxSize(26) 36 tableAuth.ColMap("Code").SetMaxSize(128) 37 tableAuth.ColMap("RedirectUri").SetMaxSize(256) 38 tableAuth.ColMap("State").SetMaxSize(1024) 39 tableAuth.ColMap("Scope").SetMaxSize(128) 40 41 tableAccess := db.AddTableWithName(model.AccessData{}, "OAuthAccessData").SetKeys(false, "Token") 42 tableAccess.ColMap("ClientId").SetMaxSize(26) 43 tableAccess.ColMap("UserId").SetMaxSize(26) 44 tableAccess.ColMap("Token").SetMaxSize(26) 45 tableAccess.ColMap("RefreshToken").SetMaxSize(26) 46 tableAccess.ColMap("RedirectUri").SetMaxSize(256) 47 tableAccess.ColMap("Scope").SetMaxSize(128) 48 tableAccess.SetUniqueTogether("ClientId", "UserId") 49 } 50 51 return as 52 } 53 54 func (as SqlOAuthStore) CreateIndexesIfNotExists() { 55 as.CreateIndexIfNotExists("idx_oauthapps_creator_id", "OAuthApps", "CreatorId") 56 as.CreateIndexIfNotExists("idx_oauthaccessdata_client_id", "OAuthAccessData", "ClientId") 57 as.CreateIndexIfNotExists("idx_oauthaccessdata_user_id", "OAuthAccessData", "UserId") 58 as.CreateIndexIfNotExists("idx_oauthaccessdata_refresh_token", "OAuthAccessData", "RefreshToken") 59 as.CreateIndexIfNotExists("idx_oauthauthdata_client_id", "OAuthAuthData", "Code") 60 } 61 62 func (as SqlOAuthStore) SaveApp(app *model.OAuthApp) store.StoreChannel { 63 return store.Do(func(result *store.StoreResult) { 64 if len(app.Id) > 0 { 65 result.Err = model.NewAppError("SqlOAuthStore.SaveApp", "store.sql_oauth.save_app.existing.app_error", nil, "app_id="+app.Id, http.StatusBadRequest) 66 return 67 } 68 69 app.PreSave() 70 if result.Err = app.IsValid(); result.Err != nil { 71 return 72 } 73 74 if err := as.GetMaster().Insert(app); err != nil { 75 result.Err = model.NewAppError("SqlOAuthStore.SaveApp", "store.sql_oauth.save_app.save.app_error", nil, "app_id="+app.Id+", "+err.Error(), http.StatusInternalServerError) 76 } else { 77 result.Data = app 78 } 79 }) 80 } 81 82 func (as SqlOAuthStore) UpdateApp(app *model.OAuthApp) store.StoreChannel { 83 return store.Do(func(result *store.StoreResult) { 84 app.PreUpdate() 85 86 if result.Err = app.IsValid(); result.Err != nil { 87 return 88 } 89 90 if oldAppResult, err := as.GetMaster().Get(model.OAuthApp{}, app.Id); err != nil { 91 result.Err = model.NewAppError("SqlOAuthStore.UpdateApp", "store.sql_oauth.update_app.finding.app_error", nil, "app_id="+app.Id+", "+err.Error(), http.StatusInternalServerError) 92 } else if oldAppResult == nil { 93 result.Err = model.NewAppError("SqlOAuthStore.UpdateApp", "store.sql_oauth.update_app.find.app_error", nil, "app_id="+app.Id, http.StatusBadRequest) 94 } else { 95 oldApp := oldAppResult.(*model.OAuthApp) 96 app.CreateAt = oldApp.CreateAt 97 app.CreatorId = oldApp.CreatorId 98 99 if count, err := as.GetMaster().Update(app); err != nil { 100 result.Err = model.NewAppError("SqlOAuthStore.UpdateApp", "store.sql_oauth.update_app.updating.app_error", nil, "app_id="+app.Id+", "+err.Error(), http.StatusInternalServerError) 101 } else if count != 1 { 102 result.Err = model.NewAppError("SqlOAuthStore.UpdateApp", "store.sql_oauth.update_app.update.app_error", nil, "app_id="+app.Id, http.StatusBadRequest) 103 } else { 104 result.Data = [2]*model.OAuthApp{app, oldApp} 105 } 106 } 107 }) 108 } 109 110 func (as SqlOAuthStore) GetApp(id string) store.StoreChannel { 111 return store.Do(func(result *store.StoreResult) { 112 if obj, err := as.GetReplica().Get(model.OAuthApp{}, id); err != nil { 113 result.Err = model.NewAppError("SqlOAuthStore.GetApp", "store.sql_oauth.get_app.finding.app_error", nil, "app_id="+id+", "+err.Error(), http.StatusInternalServerError) 114 } else if obj == nil { 115 result.Err = model.NewAppError("SqlOAuthStore.GetApp", "store.sql_oauth.get_app.find.app_error", nil, "app_id="+id, http.StatusNotFound) 116 } else { 117 result.Data = obj.(*model.OAuthApp) 118 } 119 }) 120 } 121 122 func (as SqlOAuthStore) GetAppByUser(userId string, offset, limit int) store.StoreChannel { 123 return store.Do(func(result *store.StoreResult) { 124 var apps []*model.OAuthApp 125 126 if _, err := as.GetReplica().Select(&apps, "SELECT * FROM OAuthApps WHERE CreatorId = :UserId LIMIT :Limit OFFSET :Offset", map[string]interface{}{"UserId": userId, "Offset": offset, "Limit": limit}); err != nil { 127 result.Err = model.NewAppError("SqlOAuthStore.GetAppByUser", "store.sql_oauth.get_app_by_user.find.app_error", nil, "user_id="+userId+", "+err.Error(), http.StatusInternalServerError) 128 } 129 130 result.Data = apps 131 }) 132 } 133 134 func (as SqlOAuthStore) GetApps(offset, limit int) store.StoreChannel { 135 return store.Do(func(result *store.StoreResult) { 136 var apps []*model.OAuthApp 137 138 if _, err := as.GetReplica().Select(&apps, "SELECT * FROM OAuthApps LIMIT :Limit OFFSET :Offset", map[string]interface{}{"Offset": offset, "Limit": limit}); err != nil { 139 result.Err = model.NewAppError("SqlOAuthStore.GetAppByUser", "store.sql_oauth.get_apps.find.app_error", nil, "err="+err.Error(), http.StatusInternalServerError) 140 } 141 142 result.Data = apps 143 }) 144 } 145 146 func (as SqlOAuthStore) GetAuthorizedApps(userId string, offset, limit int) store.StoreChannel { 147 return store.Do(func(result *store.StoreResult) { 148 var apps []*model.OAuthApp 149 150 if _, err := as.GetReplica().Select(&apps, 151 `SELECT o.* FROM OAuthApps AS o INNER JOIN 152 Preferences AS p ON p.Name=o.Id AND p.UserId=:UserId LIMIT :Limit OFFSET :Offset`, map[string]interface{}{"UserId": userId, "Offset": offset, "Limit": limit}); err != nil { 153 result.Err = model.NewAppError("SqlOAuthStore.GetAuthorizedApps", "store.sql_oauth.get_apps.find.app_error", nil, "err="+err.Error(), http.StatusInternalServerError) 154 } 155 156 result.Data = apps 157 }) 158 } 159 160 func (as SqlOAuthStore) DeleteApp(id string) store.StoreChannel { 161 return store.Do(func(result *store.StoreResult) { 162 // wrap in a transaction so that if one fails, everything fails 163 transaction, err := as.GetMaster().Begin() 164 if err != nil { 165 result.Err = model.NewAppError("SqlOAuthStore.DeleteApp", "store.sql_oauth.delete.open_transaction.app_error", nil, err.Error(), http.StatusInternalServerError) 166 } else { 167 defer finalizeTransaction(transaction) 168 if extrasResult := as.deleteApp(transaction, id); extrasResult.Err != nil { 169 *result = extrasResult 170 } 171 172 if result.Err == nil { 173 if err := transaction.Commit(); err != nil { 174 // don't need to rollback here since the transaction is already closed 175 result.Err = model.NewAppError("SqlOAuthStore.DeleteApp", "store.sql_oauth.delete.commit_transaction.app_error", nil, err.Error(), http.StatusInternalServerError) 176 } 177 } 178 } 179 }) 180 } 181 182 func (as SqlOAuthStore) SaveAccessData(accessData *model.AccessData) store.StoreChannel { 183 return store.Do(func(result *store.StoreResult) { 184 if result.Err = accessData.IsValid(); result.Err != nil { 185 return 186 } 187 188 if err := as.GetMaster().Insert(accessData); err != nil { 189 result.Err = model.NewAppError("SqlOAuthStore.SaveAccessData", "store.sql_oauth.save_access_data.app_error", nil, err.Error(), http.StatusInternalServerError) 190 } else { 191 result.Data = accessData 192 } 193 }) 194 } 195 196 func (as SqlOAuthStore) GetAccessData(token string) store.StoreChannel { 197 return store.Do(func(result *store.StoreResult) { 198 accessData := model.AccessData{} 199 200 if err := as.GetReplica().SelectOne(&accessData, "SELECT * FROM OAuthAccessData WHERE Token = :Token", map[string]interface{}{"Token": token}); err != nil { 201 result.Err = model.NewAppError("SqlOAuthStore.GetAccessData", "store.sql_oauth.get_access_data.app_error", nil, err.Error(), http.StatusInternalServerError) 202 } else { 203 result.Data = &accessData 204 } 205 }) 206 } 207 208 func (as SqlOAuthStore) GetAccessDataByUserForApp(userId, clientId string) store.StoreChannel { 209 return store.Do(func(result *store.StoreResult) { 210 var accessData []*model.AccessData 211 212 if _, err := as.GetReplica().Select(&accessData, 213 "SELECT * FROM OAuthAccessData WHERE UserId = :UserId AND ClientId = :ClientId", 214 map[string]interface{}{"UserId": userId, "ClientId": clientId}); err != nil { 215 result.Err = model.NewAppError("SqlOAuthStore.GetAccessDataByUserForApp", "store.sql_oauth.get_access_data_by_user_for_app.app_error", nil, "user_id="+userId+" client_id="+clientId, http.StatusInternalServerError) 216 } else { 217 result.Data = accessData 218 } 219 }) 220 } 221 222 func (as SqlOAuthStore) GetAccessDataByRefreshToken(token string) store.StoreChannel { 223 return store.Do(func(result *store.StoreResult) { 224 accessData := model.AccessData{} 225 226 if err := as.GetReplica().SelectOne(&accessData, "SELECT * FROM OAuthAccessData WHERE RefreshToken = :Token", map[string]interface{}{"Token": token}); err != nil { 227 result.Err = model.NewAppError("SqlOAuthStore.GetAccessData", "store.sql_oauth.get_access_data.app_error", nil, err.Error(), http.StatusInternalServerError) 228 } else { 229 result.Data = &accessData 230 } 231 }) 232 } 233 234 func (as SqlOAuthStore) GetPreviousAccessData(userId, clientId string) store.StoreChannel { 235 return store.Do(func(result *store.StoreResult) { 236 accessData := model.AccessData{} 237 238 if err := as.GetReplica().SelectOne(&accessData, "SELECT * FROM OAuthAccessData WHERE ClientId = :ClientId AND UserId = :UserId", 239 map[string]interface{}{"ClientId": clientId, "UserId": userId}); err != nil { 240 if strings.Contains(err.Error(), "no rows") { 241 result.Data = nil 242 } else { 243 result.Err = model.NewAppError("SqlOAuthStore.GetPreviousAccessData", "store.sql_oauth.get_previous_access_data.app_error", nil, err.Error(), http.StatusNotFound) 244 } 245 } else { 246 result.Data = &accessData 247 } 248 }) 249 } 250 251 func (as SqlOAuthStore) UpdateAccessData(accessData *model.AccessData) store.StoreChannel { 252 return store.Do(func(result *store.StoreResult) { 253 if result.Err = accessData.IsValid(); result.Err != nil { 254 return 255 } 256 257 if _, err := as.GetMaster().Exec("UPDATE OAuthAccessData SET Token = :Token, ExpiresAt = :ExpiresAt, RefreshToken = :RefreshToken WHERE ClientId = :ClientId AND UserID = :UserId", 258 map[string]interface{}{"Token": accessData.Token, "ExpiresAt": accessData.ExpiresAt, "RefreshToken": accessData.RefreshToken, "ClientId": accessData.ClientId, "UserId": accessData.UserId}); err != nil { 259 result.Err = model.NewAppError("SqlOAuthStore.Update", "store.sql_oauth.update_access_data.app_error", nil, 260 "clientId="+accessData.ClientId+",userId="+accessData.UserId+", "+err.Error(), http.StatusInternalServerError) 261 } else { 262 result.Data = accessData 263 } 264 }) 265 } 266 267 func (as SqlOAuthStore) RemoveAccessData(token string) store.StoreChannel { 268 return store.Do(func(result *store.StoreResult) { 269 if _, err := as.GetMaster().Exec("DELETE FROM OAuthAccessData WHERE Token = :Token", map[string]interface{}{"Token": token}); err != nil { 270 result.Err = model.NewAppError("SqlOAuthStore.RemoveAccessData", "store.sql_oauth.remove_access_data.app_error", nil, "err="+err.Error(), http.StatusInternalServerError) 271 } 272 }) 273 } 274 275 func (as SqlOAuthStore) SaveAuthData(authData *model.AuthData) store.StoreChannel { 276 return store.Do(func(result *store.StoreResult) { 277 authData.PreSave() 278 if result.Err = authData.IsValid(); result.Err != nil { 279 return 280 } 281 282 if err := as.GetMaster().Insert(authData); err != nil { 283 result.Err = model.NewAppError("SqlOAuthStore.SaveAuthData", "store.sql_oauth.save_auth_data.app_error", nil, err.Error(), http.StatusInternalServerError) 284 } else { 285 result.Data = authData 286 } 287 }) 288 } 289 290 func (as SqlOAuthStore) GetAuthData(code string) store.StoreChannel { 291 return store.Do(func(result *store.StoreResult) { 292 if obj, err := as.GetReplica().Get(model.AuthData{}, code); err != nil { 293 result.Err = model.NewAppError("SqlOAuthStore.GetAuthData", "store.sql_oauth.get_auth_data.finding.app_error", nil, err.Error(), http.StatusInternalServerError) 294 } else if obj == nil { 295 result.Err = model.NewAppError("SqlOAuthStore.GetAuthData", "store.sql_oauth.get_auth_data.find.app_error", nil, "", http.StatusNotFound) 296 } else { 297 result.Data = obj.(*model.AuthData) 298 } 299 }) 300 } 301 302 func (as SqlOAuthStore) RemoveAuthData(code string) store.StoreChannel { 303 return store.Do(func(result *store.StoreResult) { 304 _, err := as.GetMaster().Exec("DELETE FROM OAuthAuthData WHERE Code = :Code", map[string]interface{}{"Code": code}) 305 if err != nil { 306 result.Err = model.NewAppError("SqlOAuthStore.RemoveAuthData", "store.sql_oauth.remove_auth_data.app_error", nil, "err="+err.Error(), http.StatusInternalServerError) 307 } 308 }) 309 } 310 311 func (as SqlOAuthStore) PermanentDeleteAuthDataByUser(userId string) store.StoreChannel { 312 return store.Do(func(result *store.StoreResult) { 313 _, err := as.GetMaster().Exec("DELETE FROM OAuthAccessData WHERE UserId = :UserId", map[string]interface{}{"UserId": userId}) 314 if err != nil { 315 result.Err = model.NewAppError("SqlOAuthStore.RemoveAuthDataByUserId", "store.sql_oauth.permanent_delete_auth_data_by_user.app_error", nil, "err="+err.Error(), http.StatusInternalServerError) 316 } 317 }) 318 } 319 320 func (as SqlOAuthStore) deleteApp(transaction *gorp.Transaction, clientId string) store.StoreResult { 321 result := store.StoreResult{} 322 323 if _, err := transaction.Exec("DELETE FROM OAuthApps WHERE Id = :Id", map[string]interface{}{"Id": clientId}); err != nil { 324 result.Err = model.NewAppError("SqlOAuthStore.DeleteApp", "store.sql_oauth.delete_app.app_error", nil, "id="+clientId+", err="+err.Error(), http.StatusInternalServerError) 325 return result 326 } 327 328 return as.deleteOAuthAppSessions(transaction, clientId) 329 } 330 331 func (as SqlOAuthStore) deleteOAuthAppSessions(transaction *gorp.Transaction, clientId string) store.StoreResult { 332 result := store.StoreResult{} 333 334 query := "" 335 if as.DriverName() == model.DATABASE_DRIVER_POSTGRES { 336 query = "DELETE FROM Sessions s USING OAuthAccessData o WHERE o.Token = s.Token AND o.ClientId = :Id" 337 } else if as.DriverName() == model.DATABASE_DRIVER_MYSQL { 338 query = "DELETE s.* FROM Sessions s INNER JOIN OAuthAccessData o ON o.Token = s.Token WHERE o.ClientId = :Id" 339 } 340 341 if _, err := transaction.Exec(query, map[string]interface{}{"Id": clientId}); err != nil { 342 result.Err = model.NewAppError("SqlOAuthStore.DeleteApp", "store.sql_oauth.delete_app.app_error", nil, "id="+clientId+", err="+err.Error(), http.StatusInternalServerError) 343 return result 344 } 345 346 return as.deleteOAuthTokens(transaction, clientId) 347 } 348 349 func (as SqlOAuthStore) deleteOAuthTokens(transaction *gorp.Transaction, clientId string) store.StoreResult { 350 result := store.StoreResult{} 351 352 if _, err := transaction.Exec("DELETE FROM OAuthAccessData WHERE ClientId = :Id", map[string]interface{}{"Id": clientId}); err != nil { 353 result.Err = model.NewAppError("SqlOAuthStore.DeleteApp", "store.sql_oauth.delete_app.app_error", nil, "id="+clientId+", err="+err.Error(), http.StatusInternalServerError) 354 return result 355 } 356 357 return as.deleteAppExtras(transaction, clientId) 358 } 359 360 func (as SqlOAuthStore) deleteAppExtras(transaction *gorp.Transaction, clientId string) store.StoreResult { 361 result := store.StoreResult{} 362 363 if _, err := transaction.Exec( 364 `DELETE FROM 365 Preferences 366 WHERE 367 Category = :Category 368 AND Name = :Name`, map[string]interface{}{"Category": model.PREFERENCE_CATEGORY_AUTHORIZED_OAUTH_APP, "Name": clientId}); err != nil { 369 result.Err = model.NewAppError("SqlOAuthStore.DeleteApp", "store.sql_preference.delete.app_error", nil, err.Error(), http.StatusInternalServerError) 370 return result 371 } 372 373 return result 374 }