коммит произвёл
GitHub
родитель
f596064dff
Коммит
93a537a636
124
app/oauth.go
124
app/oauth.go
@@ -35,14 +35,40 @@ func (a *App) CreateOAuthApp(app *model.OAuthApp) (*model.OAuthApp, *model.AppEr
|
||||
|
||||
app.ClientSecret = model.NewId()
|
||||
|
||||
return a.Srv().Store.OAuth().SaveApp(app)
|
||||
oauthApp, err := a.Srv().Store.OAuth().SaveApp(app)
|
||||
if err != nil {
|
||||
var appErr *model.AppError
|
||||
var invErr *store.ErrInvalidInput
|
||||
switch {
|
||||
case errors.As(err, &appErr):
|
||||
return nil, appErr
|
||||
case errors.As(err, &invErr):
|
||||
return nil, model.NewAppError("CreateOAuthApp", "app.oauth.save_app.existing.app_error", nil, invErr.Error(), http.StatusBadRequest)
|
||||
default:
|
||||
return nil, model.NewAppError("CreateOAuthApp", "app.oauth.save_app.save.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
return oauthApp, nil
|
||||
}
|
||||
|
||||
func (a *App) GetOAuthApp(appId string) (*model.OAuthApp, *model.AppError) {
|
||||
if !*a.Config().ServiceSettings.EnableOAuthServiceProvider {
|
||||
return nil, model.NewAppError("GetOAuthApp", "api.oauth.allow_oauth.turn_off.app_error", nil, "", http.StatusNotImplemented)
|
||||
}
|
||||
return a.Srv().Store.OAuth().GetApp(appId)
|
||||
|
||||
oauthApp, err := a.Srv().Store.OAuth().GetApp(appId)
|
||||
if err != nil {
|
||||
var nfErr *store.ErrNotFound
|
||||
switch {
|
||||
case errors.As(err, &nfErr):
|
||||
return nil, model.NewAppError("GetOAuthApp", "app.oauth.get_app.find.app_error", nil, nfErr.Error(), http.StatusNotFound)
|
||||
default:
|
||||
return nil, model.NewAppError("GetOAuthApp", "app.oauth.get_app.finding.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
return oauthApp, nil
|
||||
}
|
||||
|
||||
func (a *App) UpdateOauthApp(oldApp, updatedApp *model.OAuthApp) (*model.OAuthApp, *model.AppError) {
|
||||
@@ -55,7 +81,21 @@ func (a *App) UpdateOauthApp(oldApp, updatedApp *model.OAuthApp) (*model.OAuthAp
|
||||
updatedApp.CreateAt = oldApp.CreateAt
|
||||
updatedApp.ClientSecret = oldApp.ClientSecret
|
||||
|
||||
return a.Srv().Store.OAuth().UpdateApp(updatedApp)
|
||||
oauthApp, err := a.Srv().Store.OAuth().UpdateApp(updatedApp)
|
||||
if err != nil {
|
||||
var appErr *model.AppError
|
||||
var invErr *store.ErrInvalidInput
|
||||
switch {
|
||||
case errors.As(err, &appErr):
|
||||
return nil, appErr
|
||||
case errors.As(err, &invErr):
|
||||
return nil, model.NewAppError("UpdateOauthApp", "app.oauth.update_app.find.app_error", nil, invErr.Error(), http.StatusBadRequest)
|
||||
default:
|
||||
return nil, model.NewAppError("UpdateOauthApp", "app.oauth.update_app.updating.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
return oauthApp, nil
|
||||
}
|
||||
|
||||
func (a *App) DeleteOAuthApp(appId string) *model.AppError {
|
||||
@@ -64,7 +104,7 @@ func (a *App) DeleteOAuthApp(appId string) *model.AppError {
|
||||
}
|
||||
|
||||
if err := a.Srv().Store.OAuth().DeleteApp(appId); err != nil {
|
||||
return err
|
||||
return model.NewAppError("DeleteOAuthApp", "app.oauth.delete_app.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
if err := a.Srv().InvalidateAllCaches(); err != nil {
|
||||
@@ -79,7 +119,12 @@ func (a *App) GetOAuthApps(page, perPage int) ([]*model.OAuthApp, *model.AppErro
|
||||
return nil, model.NewAppError("GetOAuthApps", "api.oauth.allow_oauth.turn_off.app_error", nil, "", http.StatusNotImplemented)
|
||||
}
|
||||
|
||||
return a.Srv().Store.OAuth().GetApps(page*perPage, perPage)
|
||||
oauthApps, err := a.Srv().Store.OAuth().GetApps(page*perPage, perPage)
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("GetOAuthApps", "app.oauth.get_apps.find.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return oauthApps, nil
|
||||
}
|
||||
|
||||
func (a *App) GetOAuthAppsByCreator(userId string, page, perPage int) ([]*model.OAuthApp, *model.AppError) {
|
||||
@@ -87,7 +132,12 @@ func (a *App) GetOAuthAppsByCreator(userId string, page, perPage int) ([]*model.
|
||||
return nil, model.NewAppError("GetOAuthAppsByUser", "api.oauth.allow_oauth.turn_off.app_error", nil, "", http.StatusNotImplemented)
|
||||
}
|
||||
|
||||
return a.Srv().Store.OAuth().GetAppByUser(userId, page*perPage, perPage)
|
||||
oauthApps, err := a.Srv().Store.OAuth().GetAppByUser(userId, page*perPage, perPage)
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("GetOAuthAppsByCreator", "app.oauth.get_app_by_user.find.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return oauthApps, nil
|
||||
}
|
||||
|
||||
func (a *App) GetOAuthImplicitRedirect(userId string, authRequest *model.AuthorizeRequest) (string, *model.AppError) {
|
||||
@@ -126,9 +176,15 @@ func (a *App) AllowOAuthAppAccessToUser(userId string, authRequest *model.Author
|
||||
authRequest.Scope = model.DEFAULT_SCOPE
|
||||
}
|
||||
|
||||
oauthApp, err := a.Srv().Store.OAuth().GetApp(authRequest.ClientId)
|
||||
if err != nil {
|
||||
return "", err
|
||||
oauthApp, nErr := a.Srv().Store.OAuth().GetApp(authRequest.ClientId)
|
||||
if nErr != nil {
|
||||
var nfErr *store.ErrNotFound
|
||||
switch {
|
||||
case errors.As(nErr, &nfErr):
|
||||
return "", model.NewAppError("AllowOAuthAppAccessToUser", "app.oauth.get_app.find.app_error", nil, nfErr.Error(), http.StatusNotFound)
|
||||
default:
|
||||
return "", model.NewAppError("AllowOAuthAppAccessToUser", "app.oauth.get_app.finding.app_error", nil, nErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
if !oauthApp.IsValidRedirectURL(authRequest.RedirectUri) {
|
||||
@@ -136,7 +192,7 @@ func (a *App) AllowOAuthAppAccessToUser(userId string, authRequest *model.Author
|
||||
}
|
||||
|
||||
var redirectURI string
|
||||
|
||||
var err *model.AppError
|
||||
switch authRequest.ResponseType {
|
||||
case model.AUTHCODE_RESPONSE_TYPE:
|
||||
redirectURI, err = a.GetOAuthCodeRedirect(userId, authRequest)
|
||||
@@ -202,8 +258,8 @@ func (a *App) GetOAuthAccessTokenForCodeFlow(clientId, grantType, redirectUri, c
|
||||
return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.disabled.app_error", nil, "", http.StatusNotImplemented)
|
||||
}
|
||||
|
||||
oauthApp, err := a.Srv().Store.OAuth().GetApp(clientId)
|
||||
if err != nil {
|
||||
oauthApp, nErr := a.Srv().Store.OAuth().GetApp(clientId)
|
||||
if nErr != nil {
|
||||
return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.credentials.app_error", nil, "", http.StatusNotFound)
|
||||
}
|
||||
|
||||
@@ -211,18 +267,19 @@ func (a *App) GetOAuthAccessTokenForCodeFlow(clientId, grantType, redirectUri, c
|
||||
return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.credentials.app_error", nil, "", http.StatusForbidden)
|
||||
}
|
||||
|
||||
var user *model.User
|
||||
var accessData *model.AccessData
|
||||
var accessRsp *model.AccessResponse
|
||||
if grantType == model.ACCESS_TOKEN_GRANT_TYPE {
|
||||
var authData *model.AuthData
|
||||
authData, err = a.Srv().Store.OAuth().GetAuthData(code)
|
||||
if err != nil {
|
||||
authData, nErr = a.Srv().Store.OAuth().GetAuthData(code)
|
||||
if nErr != nil {
|
||||
return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.expired_code.app_error", nil, "", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if authData.IsExpired() {
|
||||
a.Srv().Store.OAuth().RemoveAuthData(authData.Code)
|
||||
if nErr = a.Srv().Store.OAuth().RemoveAuthData(authData.Code); nErr != nil {
|
||||
mlog.Warn("unable to remove auth data", mlog.Err(nErr))
|
||||
}
|
||||
return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.expired_code.app_error", nil, "", http.StatusForbidden)
|
||||
}
|
||||
|
||||
@@ -230,13 +287,13 @@ func (a *App) GetOAuthAccessTokenForCodeFlow(clientId, grantType, redirectUri, c
|
||||
return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.redirect_uri.app_error", nil, "", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
user, err = a.Srv().Store.User().Get(authData.UserId)
|
||||
user, err := a.Srv().Store.User().Get(authData.UserId)
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.internal_user.app_error", nil, "", http.StatusNotFound)
|
||||
}
|
||||
|
||||
accessData, err = a.Srv().Store.OAuth().GetPreviousAccessData(user.Id, clientId)
|
||||
if err != nil {
|
||||
accessData, nErr = a.Srv().Store.OAuth().GetPreviousAccessData(user.Id, clientId)
|
||||
if nErr != nil {
|
||||
return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.internal.app_error", nil, "", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
@@ -267,8 +324,8 @@ func (a *App) GetOAuthAccessTokenForCodeFlow(clientId, grantType, redirectUri, c
|
||||
|
||||
accessData = &model.AccessData{ClientId: clientId, UserId: user.Id, Token: session.Token, RefreshToken: model.NewId(), RedirectUri: redirectUri, ExpiresAt: session.ExpiresAt, Scope: authData.Scope}
|
||||
|
||||
if _, err = a.Srv().Store.OAuth().SaveAccessData(accessData); err != nil {
|
||||
mlog.Error("error saving oauth access data in token for code flow", mlog.Err(err))
|
||||
if _, nErr = a.Srv().Store.OAuth().SaveAccessData(accessData); nErr != nil {
|
||||
mlog.Error("error saving oauth access data in token for code flow", mlog.Err(nErr))
|
||||
return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.internal_saving.app_error", nil, "", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
@@ -280,11 +337,13 @@ func (a *App) GetOAuthAccessTokenForCodeFlow(clientId, grantType, redirectUri, c
|
||||
}
|
||||
}
|
||||
|
||||
a.Srv().Store.OAuth().RemoveAuthData(authData.Code)
|
||||
if nErr = a.Srv().Store.OAuth().RemoveAuthData(authData.Code); nErr != nil {
|
||||
mlog.Warn("unable to remove auth data", mlog.Err(nErr))
|
||||
}
|
||||
} else {
|
||||
// When grantType is refresh_token
|
||||
accessData, err = a.Srv().Store.OAuth().GetAccessDataByRefreshToken(refreshToken)
|
||||
if err != nil {
|
||||
accessData, nErr = a.Srv().Store.OAuth().GetAccessDataByRefreshToken(refreshToken)
|
||||
if nErr != nil {
|
||||
return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.refresh_token.app_error", nil, "", http.StatusNotFound)
|
||||
}
|
||||
|
||||
@@ -394,7 +453,7 @@ func (a *App) GetAuthorizedAppsForUser(userId string, page, perPage int) ([]*mod
|
||||
|
||||
apps, err := a.Srv().Store.OAuth().GetAuthorizedApps(userId, page*perPage, perPage)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, model.NewAppError("GetAuthorizedAppsForUser", "app.oauth.get_apps.find.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
for k, a := range apps {
|
||||
@@ -413,7 +472,7 @@ func (a *App) DeauthorizeOAuthAppForUser(userId, appId string) *model.AppError {
|
||||
// Revoke app sessions
|
||||
accessData, err := a.Srv().Store.OAuth().GetAccessDataByUserForApp(userId, appId)
|
||||
if err != nil {
|
||||
return err
|
||||
return model.NewAppError("DeauthorizeOAuthAppForUser", "app.oauth.get_access_data_by_user_for_app.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
for _, ad := range accessData {
|
||||
@@ -422,7 +481,7 @@ func (a *App) DeauthorizeOAuthAppForUser(userId, appId string) *model.AppError {
|
||||
}
|
||||
|
||||
if err := a.Srv().Store.OAuth().RemoveAccessData(ad.Token); err != nil {
|
||||
return err
|
||||
return model.NewAppError("DeauthorizeOAuthAppForUser", "app.oauth.remove_access_data.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -441,7 +500,16 @@ func (a *App) RegenerateOAuthAppSecret(app *model.OAuthApp) (*model.OAuthApp, *m
|
||||
|
||||
app.ClientSecret = model.NewId()
|
||||
if _, err := a.Srv().Store.OAuth().UpdateApp(app); err != nil {
|
||||
return nil, err
|
||||
var appErr *model.AppError
|
||||
var invErr *store.ErrInvalidInput
|
||||
switch {
|
||||
case errors.As(err, &appErr):
|
||||
return nil, appErr
|
||||
case errors.As(err, &invErr):
|
||||
return nil, model.NewAppError("RegenerateOAuthAppSecret", "app.oauth.update_app.find.app_error", nil, invErr.Error(), http.StatusBadRequest)
|
||||
default:
|
||||
return nil, model.NewAppError("RegenerateOAuthAppSecret", "app.oauth.update_app.updating.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
return app, nil
|
||||
|
||||
@@ -90,8 +90,8 @@ func TestOAuthRevokeAccessToken(t *testing.T) {
|
||||
accessData.ClientId = model.NewId()
|
||||
accessData.ExpiresAt = session.ExpiresAt
|
||||
|
||||
_, err = th.App.Srv().Store.OAuth().SaveAccessData(accessData)
|
||||
require.Nil(t, err)
|
||||
_, nErr := th.App.Srv().Store.OAuth().SaveAccessData(accessData)
|
||||
require.Nil(t, nErr)
|
||||
|
||||
err = th.App.RevokeAccessToken(accessData.Token)
|
||||
require.Nil(t, err)
|
||||
@@ -130,8 +130,8 @@ func TestOAuthDeleteApp(t *testing.T) {
|
||||
accessData.ClientId = a1.Id
|
||||
accessData.ExpiresAt = session.ExpiresAt
|
||||
|
||||
_, err = th.App.Srv().Store.OAuth().SaveAccessData(accessData)
|
||||
require.Nil(t, err)
|
||||
_, nErr := th.App.Srv().Store.OAuth().SaveAccessData(accessData)
|
||||
require.Nil(t, nErr)
|
||||
|
||||
err = th.App.DeleteOAuthApp(a1.Id)
|
||||
require.Nil(t, err)
|
||||
|
||||
@@ -153,9 +153,9 @@ func (a *App) RevokeAllSessions(userId string) *model.AppError {
|
||||
// in the server and revoke them
|
||||
func (a *App) RevokeSessionsFromAllUsers() *model.AppError {
|
||||
// revoke tokens before sessions so they can't be used to relogin
|
||||
tErr := a.Srv().Store.OAuth().RemoveAllAccessData()
|
||||
if tErr != nil {
|
||||
return tErr
|
||||
nErr := a.Srv().Store.OAuth().RemoveAllAccessData()
|
||||
if nErr != nil {
|
||||
return model.NewAppError("RevokeSessionsFromAllUsers", "app.oauth.remove_access_data.app_error", nil, nErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
err := a.Srv().Store.Session().RemoveAllSessions()
|
||||
if err != nil {
|
||||
|
||||
@@ -1461,7 +1461,7 @@ func (a *App) PermanentDeleteUser(user *model.User) *model.AppError {
|
||||
}
|
||||
|
||||
if err := a.Srv().Store.OAuth().PermanentDeleteAuthDataByUser(user.Id); err != nil {
|
||||
return err
|
||||
return model.NewAppError("PermanentDeleteUser", "app.oauth.permanent_delete_auth_data_by_user.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
if err := a.Srv().Store.Webhook().PermanentDeleteIncomingByUser(user.Id); err != nil {
|
||||
|
||||
Ссылка в новой задаче
Block a user