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
Prev Previous commit
Next Next commit
move endpoint to /user. Add swagger documentations and proper respons…
…e type.
  • Loading branch information
Gustavo Marin committed Feb 25, 2020
commit a7d0c5cb8f9649d0db68182ead9717d2f1176483
8 changes: 0 additions & 8 deletions modules/structs/admin_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@ 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
16 changes: 16 additions & 0 deletions modules/structs/user_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,19 @@ type AccessTokenList []*AccessToken
type CreateAccessTokenOption struct {
Name string `json:"name" binding:"Required"`
}

// CreateOAuth2ApplicationOptions holds options to create an oauth2 application
// swagger:parameters userCreateOAuth2Application
type CreateOAuth2ApplicationOptions struct {
Name string `json:"name" binding:"Required"`
RedirectURIs []string `json:"redirect-uris" binding:"Required"`
}

// OAuth2Application represents an OAuth2 application.
// swagger:response OAuth2Application
type OAuth2Application struct {
Name string `json:"name"`
ClientID string `json:"client-id"`
ClientSecret string `json:"client-secret"`
RedirectURIs []string `json:"redirect-uris"`
}
7 changes: 3 additions & 4 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,9 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Combo("/:id").Get(user.GetPublicKey).
Delete(user.DeletePublicKey)
})
m.Group("/applications", func() {
m.Post("/oauth2", bind(api.CreateOAuth2ApplicationOptions{}), user.CreateOauth2Application)
}, reqToken())

m.Group("/gpg_keys", func() {
m.Combo("").Get(user.ListMyGPGKeys).
Expand Down Expand Up @@ -882,10 +885,6 @@ 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
12 changes: 12 additions & 0 deletions routers/api/v1/swagger/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package swagger
This conversation was marked as resolved.
Show resolved Hide resolved

import (
api "code.gitea.io/gitea/modules/structs"
)

// OAuth2Application
// swagger:response OAuth2Application
type swaggerResponseOAuth2Application struct {
// in:body
Body api.OAuth2Application `json:"body"`
}
35 changes: 35 additions & 0 deletions routers/api/v1/user/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,38 @@ func DeleteAccessToken(ctx *context.APIContext) {

ctx.Status(http.StatusNoContent)
}

// CreateOauth2Application is the handler to create a new OAuth2 Application for the authenticated user
func CreateOauth2Application(ctx *context.APIContext, data api.CreateOAuth2ApplicationOptions) {
// swagger:operation POST /user/applications/oauth2 userCreateOAuth2Application
// ---
// summary: creates a new OAuth2 application
// produces:
// - application/json
// parameters:
// responses:
// "200":
// "$ref": "#/responses/OAuth2Application"
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, api.OAuth2Application{
This conversation was marked as resolved.
Show resolved Hide resolved
Name: app.Name,
ClientID: app.ClientID,
ClientSecret: app.ClientSecret,
RedirectURIs: app.RedirectURIs,
})
}
21 changes: 0 additions & 21 deletions routers/api/v1/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,24 +166,3 @@ 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)
}
46 changes: 46 additions & 0 deletions templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8059,6 +8059,20 @@
}
}
},
"/user/applications/oauth2": {
"post": {
"produces": [
"application/json"
],
"summary": "creates a new OAuth2 application",
"operationId": "userCreateOAuth2Application",
"responses": {
"200": {
"$ref": "#/responses/OAuth2Application"
}
}
}
},
"/user/emails": {
"get": {
"produces": [
Expand Down Expand Up @@ -12186,6 +12200,32 @@
},
"x-go-package": "code.gitea.io/gitea/modules/structs"
},
"OAuth2Application": {
"type": "object",
"title": "OAuth2Application represents an OAuth2 application.",
"properties": {
"client-id": {
"type": "string",
"x-go-name": "ClientID"
},
"client-secret": {
"type": "string",
"x-go-name": "ClientSecret"
},
"name": {
"type": "string",
"x-go-name": "Name"
},
"redirect-uris": {
"type": "array",
"items": {
"type": "string"
},
"x-go-name": "RedirectURIs"
}
},
"x-go-package": "code.gitea.io/gitea/modules/structs"
},
"Organization": {
"description": "Organization represents an organization",
"type": "object",
Expand Down Expand Up @@ -13677,6 +13717,12 @@
}
}
},
"OAuth2Application": {
"description": "OAuth2Application",
"schema": {
"$ref": "#/definitions/OAuth2Application"
}
},
"Organization": {
"description": "Organization",
"schema": {
Expand Down