Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions cmd/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ func initializeRoutes(engine *gin.Engine) {
// Applications
engine.GET("/v2/developers/applications", middleware.RequireAuth, handlers.CreateHandler(handlers.GetUserApplications))
engine.POST("/v2/developers/applications", middleware.RequireAuth, handlers.CreateHandler(handlers.CreateNewApplication))
engine.GET("/v2/developers/applications/client/:client_id", middleware.RequireAuth, handlers.CreateHandler(handlers.GetApplicationByClientId))
engine.GET("/v2/developers/applications/:id", middleware.RequireAuth, handlers.CreateHandler(handlers.GetUserApplication))
engine.POST("/v2/developers/applications/:id", middleware.RequireAuth, handlers.CreateHandler(handlers.UpdateApplication))
engine.DELETE("/v2/developers/applications/:id", middleware.RequireAuth, handlers.CreateHandler(handlers.DeleteUserApplication))
Expand Down
15 changes: 15 additions & 0 deletions db/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ func GetApplicationById(id int) (*Application, error) {
return application, nil
}

// GetActiveApplicationByClientId retrieves an active application by client id.
func GetActiveApplicationByClientId(clientId string) (*Application, error) {
var application *Application

result := SQL.
Where("client_id = ? AND active = 1", clientId).
First(&application)

if result.Error != nil {
return nil, result.Error
}

return application, nil
}

// GetActiveApplicationByClientSecret Retrieves an active application by client secret
func GetActiveApplicationByClientSecret(secret string) (*Application, error) {
var application *Application
Expand Down
28 changes: 28 additions & 0 deletions handlers/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handlers

import (
"github.com/Quaver/api2/db"
"github.com/Quaver/api2/enums"
"github.com/Quaver/api2/stringutil"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
Expand Down Expand Up @@ -55,6 +56,33 @@ func GetUserApplication(c *gin.Context) *APIError {
return nil
}

// GetApplicationByClientId returns an active application, including its client secret.
// Endpoint: GET /v2/developers/applications/client/:client_id
func GetApplicationByClientId(c *gin.Context) *APIError {
user := getAuthedUser(c)

if user == nil {
return nil
}

if !enums.HasPrivilege(user.Privileges, enums.PrivilegeManageBuilds) {
return APIErrorForbidden("You do not have permission to access this route.")
}

application, err := db.GetActiveApplicationByClientId(c.Param("client_id"))

if err != nil && err != gorm.ErrRecordNotFound {
return APIErrorServerError("Error retrieving application from db", err)
}

if application == nil {
return APIErrorNotFound("Application")
}

c.JSON(http.StatusOK, gin.H{"application": application})
return nil
}

// CreateNewApplication Creates a new application
// Endpoint: POST /v2/developers/applications
func CreateNewApplication(c *gin.Context) *APIError {
Expand Down
Loading