Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API endpoints to manage OAuth2 Application (list/create/delete) #10437

Merged
merged 15 commits into from
Feb 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add API endpoint to create OAuth2 Application.
  • Loading branch information
Gustavo Marin committed Feb 25, 2020
commit f1f2a57fce8413d032d2f86171bc4445fa2e93ea
8 changes: 8 additions & 0 deletions modules/structs/admin_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ type CreateUserOption struct {
SendNotify bool `json:"send_notify"`
}

// CreateOauthApplicationOption create user options
type CreateOauthApplicationOption struct {
// required: true
Name string `json:"name" binding:"Required"`
// required: true
RedirectURIs []string `json:"redirect-uri" binding:"Required"`
}

// EditUserOption edit user options
type EditUserOption struct {
SourceID int64 `json:"source_id"`
Expand Down
4 changes: 4 additions & 0 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,10 @@ func RegisterRoutes(m *macaron.Macaron) {
})
}, orgAssignment(false, true), reqToken(), reqTeamMembership())

m.Group("/applications", func() {
m.Post("/oauth2", bind(api.CreateOauthApplicationOption{}), user.CreateOauth2Application)
}, reqToken())

m.Any("/*", func(ctx *context.APIContext) {
ctx.NotFound()
})
Expand Down
21 changes: 21 additions & 0 deletions routers/api/v1/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,24 @@ func GetUserHeatmapData(ctx *context.APIContext) {
}
ctx.JSON(http.StatusOK, heatmap)
}

// CreateOauth2Application is the handler to create a new OAuth2 Application for the authenticated user
func CreateOauth2Application(ctx *context.APIContext, data api.CreateOauthApplicationOption) {
app, err := models.CreateOAuth2Application(models.CreateOAuth2ApplicationOptions{
Name: data.Name,
UserID: ctx.User.ID,
RedirectURIs: data.RedirectURIs,
})
if err != nil {
ctx.Error(http.StatusBadRequest, "", "error creating oauth2 application")
return
}
secret, err := app.GenerateClientSecret()
if err != nil {
ctx.Error(http.StatusBadRequest, "", "error creating application secret")
return
}
app.ClientSecret = secret

ctx.JSON(http.StatusOK, app)
}