Refactor UpdateOAuth2Application (#11034)

Following on from #11008 refactor UpdateOAuth2Application
pull/11262/head
6543 5 years ago committed by GitHub
parent c25969e694
commit ab69b9b1a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 34
      models/oauth2_application.go
  2. 10
      routers/api/v1/user/app.go
  3. 8
      routers/user/setting/oauth2.go

@ -196,18 +196,34 @@ type UpdateOAuth2ApplicationOptions struct {
} }
// UpdateOAuth2Application updates an oauth2 application // UpdateOAuth2Application updates an oauth2 application
func UpdateOAuth2Application(opts UpdateOAuth2ApplicationOptions) error { func UpdateOAuth2Application(opts UpdateOAuth2ApplicationOptions) (*OAuth2Application, error) {
return updateOAuth2Application(x, opts) sess := x.NewSession()
if err := sess.Begin(); err != nil {
return nil, err
} }
defer sess.Close()
func updateOAuth2Application(e Engine, opts UpdateOAuth2ApplicationOptions) error { app, err := getOAuth2ApplicationByID(sess, opts.ID)
app := &OAuth2Application{ if err != nil {
ID: opts.ID, return nil, err
UID: opts.UserID,
Name: opts.Name,
RedirectURIs: opts.RedirectURIs,
} }
if _, err := e.ID(opts.ID).Update(app); err != nil { if app.UID != opts.UserID {
return nil, fmt.Errorf("UID missmatch")
}
app.Name = opts.Name
app.RedirectURIs = opts.RedirectURIs
if err = updateOAuth2Application(sess, app); err != nil {
return nil, err
}
app.ClientSecret = ""
return app, sess.Commit()
}
func updateOAuth2Application(e Engine, app *OAuth2Application) error {
if _, err := e.ID(app.ID).Update(app); err != nil {
return err return err
} }
return nil return nil

@ -301,17 +301,12 @@ func UpdateOauth2Application(ctx *context.APIContext, data api.CreateOAuth2Appli
// "$ref": "#/responses/OAuth2Application" // "$ref": "#/responses/OAuth2Application"
appID := ctx.ParamsInt64(":id") appID := ctx.ParamsInt64(":id")
err := models.UpdateOAuth2Application(models.UpdateOAuth2ApplicationOptions{ app, err := models.UpdateOAuth2Application(models.UpdateOAuth2ApplicationOptions{
Name: data.Name, Name: data.Name,
UserID: ctx.User.ID, UserID: ctx.User.ID,
ID: appID, ID: appID,
RedirectURIs: data.RedirectURIs, RedirectURIs: data.RedirectURIs,
}) })
if err != nil {
ctx.Error(http.StatusBadRequest, "", "error updating oauth2 application")
return
}
app, err := models.GetOAuth2ApplicationByID(appID)
if err != nil { if err != nil {
if models.IsErrOauthClientIDInvalid(err) || models.IsErrOAuthApplicationNotFound(err) { if models.IsErrOauthClientIDInvalid(err) || models.IsErrOAuthApplicationNotFound(err) {
ctx.NotFound() ctx.NotFound()
@ -320,12 +315,11 @@ func UpdateOauth2Application(ctx *context.APIContext, data api.CreateOAuth2Appli
} }
return return
} }
secret, err := app.GenerateClientSecret() app.ClientSecret, err = app.GenerateClientSecret()
if err != nil { if err != nil {
ctx.Error(http.StatusBadRequest, "", "error updating application secret") ctx.Error(http.StatusBadRequest, "", "error updating application secret")
return return
} }
app.ClientSecret = secret
ctx.JSON(http.StatusOK, convert.ToOAuth2Application(app)) ctx.JSON(http.StatusOK, convert.ToOAuth2Application(app))
} }

@ -62,7 +62,8 @@ func OAuthApplicationsEdit(ctx *context.Context, form auth.EditOAuth2Application
return return
} }
// TODO validate redirect URI // TODO validate redirect URI
if err := models.UpdateOAuth2Application(models.UpdateOAuth2ApplicationOptions{ var err error
if ctx.Data["App"], err = models.UpdateOAuth2Application(models.UpdateOAuth2ApplicationOptions{
ID: ctx.ParamsInt64("id"), ID: ctx.ParamsInt64("id"),
Name: form.Name, Name: form.Name,
RedirectURIs: []string{form.RedirectURI}, RedirectURIs: []string{form.RedirectURI},
@ -71,11 +72,6 @@ func OAuthApplicationsEdit(ctx *context.Context, form auth.EditOAuth2Application
ctx.ServerError("UpdateOAuth2Application", err) ctx.ServerError("UpdateOAuth2Application", err)
return return
} }
var err error
if ctx.Data["App"], err = models.GetOAuth2ApplicationByID(ctx.ParamsInt64("id")); err != nil {
ctx.ServerError("GetOAuth2ApplicationByID", err)
return
}
ctx.Flash.Success(ctx.Tr("settings.update_oauth2_application_success")) ctx.Flash.Success(ctx.Tr("settings.update_oauth2_application_success"))
ctx.HTML(200, tplSettingsOAuthApplications) ctx.HTML(200, tplSettingsOAuthApplications)
} }

Loading…
Cancel
Save