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
add methods to list OAuth2 apps and delete an existing OAuth2 app by ID.
  • Loading branch information
Gustavo Marin committed Feb 25, 2020
commit 23c45c08b670f10e6c8491e478ea228e02e253f1
17 changes: 17 additions & 0 deletions models/oauth2_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,23 @@ func DeleteOAuth2Application(id, userid int64) error {
return sess.Commit()
}

// ListOAuth2Applications returns a list of oauth2 applications belongs to given user.
func ListOAuth2Applications(uid int64, listOptions ListOptions) ([]*OAuth2Application, error) {
sess := x.
Where("uid=?", uid).
Desc("id")

if listOptions.Page == 0 {
This conversation was marked as resolved.
Show resolved Hide resolved
sess = listOptions.setSessionPagination(sess)

apps := make([]*OAuth2Application, 0, listOptions.PageSize)
return apps, sess.Find(&apps)
}

apps := make([]*OAuth2Application, 0, 5)
return apps, sess.Find(&apps)
}

//////////////////////////////////////////////////////

// OAuth2AuthorizationCode is a code to obtain an access token in combination with the client secret once. It has a limited lifetime.
Expand Down
5 changes: 5 additions & 0 deletions modules/structs/user_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@ type CreateOAuth2ApplicationOptions struct {
// OAuth2Application represents an OAuth2 application.
// swagger:response OAuth2Application
type OAuth2Application struct {
ID int64 `json:"id"`
Name string `json:"name"`
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
RedirectURIs []string `json:"redirect_uris"`
}

// OAuth2ApplicationList represents a list of OAuth2 applications.
// swagger:response OAuth2ApplicationList
type OAuth2ApplicationList []*OAuth2Application
5 changes: 4 additions & 1 deletion routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,10 @@ func RegisterRoutes(m *macaron.Macaron) {
Delete(user.DeletePublicKey)
})
m.Group("/applications", func() {
m.Post("/oauth2", bind(api.CreateOAuth2ApplicationOptions{}), user.CreateOauth2Application)
m.Combo("/oauth2").
Get(user.ListOauth2Applications).
Post(bind(api.CreateOAuth2ApplicationOptions{}), user.CreateOauth2Application)
m.Delete("/oauth2/:id", user.DeleteOauth2Application)
}, reqToken())

m.Group("/gpg_keys", func() {
Expand Down
65 changes: 65 additions & 0 deletions routers/api/v1/user/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,74 @@ func CreateOauth2Application(ctx *context.APIContext, data api.CreateOAuth2Appli
app.ClientSecret = secret

ctx.JSON(http.StatusCreated, api.OAuth2Application{
ID: app.ID,
Name: app.Name,
ClientID: app.ClientID,
ClientSecret: app.ClientSecret,
RedirectURIs: app.RedirectURIs,
})
}

// ListOauth2Applications list all the Oauth2 application
func ListOauth2Applications(ctx *context.APIContext) {
// swagger:operation GET /user/applications/oauth2 user userGetOauth2Application
// ---
// summary: List the authenticated user's oauth2 applications
// produces:
// - application/json
// parameters:
// - name: page
// in: query
// description: page number of results to return (1-based)
// type: integer
// - name: limit
// in: query
// description: page size of results, maximum page size is 50
// type: integer
// responses:
// "200":
// "$ref": "#/responses/OAuth2ApplicationList"

apps, err := models.ListOAuth2Applications(ctx.User.ID, utils.GetListOptions(ctx))
if err != nil {
ctx.Error(http.StatusInternalServerError, "ListOAuth2Applications", err)
return
}

apiApps := make([]*api.OAuth2Application, len(apps))
This conversation was marked as resolved.
Show resolved Hide resolved
for i := range apps {
apiApps[i] = &api.OAuth2Application{
ID: apps[i].ID,
Name: apps[i].Name,
ClientID: apps[i].ClientID,
RedirectURIs: apps[i].RedirectURIs,
}
}
ctx.JSON(http.StatusOK, &apiApps)
}

// DeleteOauth2Application delete OAuth2 Application
func DeleteOauth2Application(ctx *context.APIContext) {
// swagger:operation DELETE /user/applications/oauth2/{id} user userDeleteOAuth2Application
// ---
// summary: delete an OAuth2 Application
// produces:
// - application/json
// parameters:
// - name: id
// in: path
// description: token to be deleted
// type: integer
// format: int64
// required: true
// responses:
// "204":
// "$ref": "#/responses/empty"
appID := ctx.ParamsInt64(":id")
if err := models.DeleteOAuth2Application(appID, ctx.User.ID); err != nil {
ctx.Error(http.StatusInternalServerError, "DeleteOauth2ApplicationByID", err)
return
}

ctx.Status(http.StatusNoContent)
}
70 changes: 70 additions & 0 deletions templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8060,6 +8060,35 @@
}
},
"/user/applications/oauth2": {
"get": {
"produces": [
"application/json"
],
"tags": [
"user"
],
"summary": "List the authenticated user's oauth2 applications",
"operationId": "userGetOauth2Application",
"parameters": [
{
"type": "integer",
"description": "page number of results to return (1-based)",
"name": "page",
"in": "query"
},
{
"type": "integer",
"description": "page size of results, maximum page size is 50",
"name": "limit",
"in": "query"
}
],
"responses": {
"200": {
"$ref": "#/responses/OAuth2ApplicationList"
}
}
},
"post": {
"produces": [
"application/json"
Expand All @@ -8086,6 +8115,33 @@
}
}
},
"/user/applications/oauth2/{id}": {
"delete": {
"produces": [
"application/json"
],
"tags": [
"user"
],
"summary": "delete an OAuth2 Application",
"operationId": "userDeleteOAuth2Application",
"parameters": [
{
"type": "integer",
"format": "int64",
"description": "token to be deleted",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"204": {
"$ref": "#/responses/empty"
}
}
}
},
"/user/emails": {
"get": {
"produces": [
Expand Down Expand Up @@ -12243,6 +12299,11 @@
"type": "string",
"x-go-name": "ClientSecret"
},
"id": {
"type": "integer",
"format": "int64",
"x-go-name": "ID"
},
"name": {
"type": "string",
"x-go-name": "Name"
Expand Down Expand Up @@ -13754,6 +13815,15 @@
"$ref": "#/definitions/OAuth2Application"
}
},
"OAuth2ApplicationList": {
"description": "OAuth2ApplicationList represents a list of OAuth2 applications.",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/OAuth2Application"
}
}
},
"Organization": {
"description": "Organization",
"schema": {
Expand Down