Skip to content

Commit

Permalink
feat(api/domain): add GET /conf/domain endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
qwqcode committed Feb 11, 2024
1 parent 2cd2165 commit cf20946
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 22 deletions.
59 changes: 52 additions & 7 deletions docs/swagger/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,35 @@ const docTemplate = `{
}
}
},
"/conf/domain": {
"get": {
"description": "Get Domain Info",
"produces": [
"application/json"
],
"tags": [
"System"
],
"summary": "Get Domain Info",
"operationId": "GetDomain",
"parameters": [
{
"type": "string",
"description": "Domain URL",
"name": "url",
"in": "query"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/handler.ResponseConfDomain"
}
}
}
}
},
"/notifies": {
"get": {
"description": "Get a list of notifies for user",
Expand Down Expand Up @@ -2387,7 +2416,7 @@ const docTemplate = `{
"application/json"
],
"tags": [
"Account"
"Auth"
],
"summary": "Get User Info",
"operationId": "GetUser",
Expand Down Expand Up @@ -2443,7 +2472,7 @@ const docTemplate = `{
"application/json"
],
"tags": [
"Account"
"Auth"
],
"summary": "Get Access Token",
"operationId": "Login",
Expand Down Expand Up @@ -2494,8 +2523,8 @@ const docTemplate = `{
]
}
},
"403": {
"description": "Forbidden",
"401": {
"description": "Unauthorized",
"schema": {
"allOf": [
{
Expand Down Expand Up @@ -2545,7 +2574,7 @@ const docTemplate = `{
"application/json"
],
"tags": [
"Account"
"Auth"
],
"summary": "Get Login Status",
"operationId": "GetUserStatus",
Expand Down Expand Up @@ -3545,8 +3574,7 @@ const docTemplate = `{
"is_pinned",
"page_key",
"rid",
"site_name",
"ua"
"site_name"
],
"properties": {
"content": {
Expand Down Expand Up @@ -4184,6 +4212,23 @@ const docTemplate = `{
}
}
},
"handler.ResponseConfDomain": {
"type": "object",
"required": [
"is_trusted",
"origin"
],
"properties": {
"is_trusted": {
"description": "Is the domain trusted",
"type": "boolean"
},
"origin": {
"description": "The origin of the domain",
"type": "string"
}
}
},
"handler.ResponseNotifyList": {
"type": "object",
"required": [
Expand Down
59 changes: 52 additions & 7 deletions docs/swagger/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,35 @@
}
}
},
"/conf/domain": {
"get": {
"description": "Get Domain Info",
"produces": [
"application/json"
],
"tags": [
"System"
],
"summary": "Get Domain Info",
"operationId": "GetDomain",
"parameters": [
{
"type": "string",
"description": "Domain URL",
"name": "url",
"in": "query"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/handler.ResponseConfDomain"
}
}
}
}
},
"/notifies": {
"get": {
"description": "Get a list of notifies for user",
Expand Down Expand Up @@ -2380,7 +2409,7 @@
"application/json"
],
"tags": [
"Account"
"Auth"
],
"summary": "Get User Info",
"operationId": "GetUser",
Expand Down Expand Up @@ -2436,7 +2465,7 @@
"application/json"
],
"tags": [
"Account"
"Auth"
],
"summary": "Get Access Token",
"operationId": "Login",
Expand Down Expand Up @@ -2487,8 +2516,8 @@
]
}
},
"403": {
"description": "Forbidden",
"401": {
"description": "Unauthorized",
"schema": {
"allOf": [
{
Expand Down Expand Up @@ -2538,7 +2567,7 @@
"application/json"
],
"tags": [
"Account"
"Auth"
],
"summary": "Get Login Status",
"operationId": "GetUserStatus",
Expand Down Expand Up @@ -3538,8 +3567,7 @@
"is_pinned",
"page_key",
"rid",
"site_name",
"ua"
"site_name"
],
"properties": {
"content": {
Expand Down Expand Up @@ -4177,6 +4205,23 @@
}
}
},
"handler.ResponseConfDomain": {
"type": "object",
"required": [
"is_trusted",
"origin"
],
"properties": {
"is_trusted": {
"description": "Is the domain trusted",
"type": "boolean"
},
"origin": {
"description": "The origin of the domain",
"type": "string"
}
}
},
"handler.ResponseNotifyList": {
"type": "object",
"required": [
Expand Down
42 changes: 36 additions & 6 deletions docs/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,6 @@ definitions:
- page_key
- rid
- site_name
- ua
type: object
handler.ParamsEmailSend:
properties:
Expand Down Expand Up @@ -764,6 +763,18 @@ definitions:
- vote_down
- vote_up
type: object
handler.ResponseConfDomain:
properties:
is_trusted:
description: Is the domain trusted
type: boolean
origin:
description: The origin of the domain
type: string
required:
- is_trusted
- origin
type: object
handler.ResponseNotifyList:
properties:
count:
Expand Down Expand Up @@ -1560,6 +1571,25 @@ paths:
summary: Get System Configs
tags:
- System
/conf/domain:
get:
description: Get Domain Info
operationId: GetDomain
parameters:
- description: Domain URL
in: query
name: url
type: string
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/handler.ResponseConfDomain'
summary: Get Domain Info
tags:
- System
/notifies:
get:
consumes:
Expand Down Expand Up @@ -2511,7 +2541,7 @@ paths:
- ApiKeyAuth: []
summary: Get User Info
tags:
- Account
- Auth
/user/access_token:
post:
consumes:
Expand Down Expand Up @@ -2548,8 +2578,8 @@ paths:
msg:
type: string
type: object
"403":
description: Forbidden
"401":
description: Unauthorized
schema:
allOf:
- $ref: '#/definitions/handler.Map'
Expand All @@ -2568,7 +2598,7 @@ paths:
type: object
summary: Get Access Token
tags:
- Account
- Auth
/user/status:
get:
description: Get user login status by header Authorization
Expand All @@ -2593,7 +2623,7 @@ paths:
- ApiKeyAuth: []
summary: Get Login Status
tags:
- Account
- Auth
/users:
post:
consumes:
Expand Down
42 changes: 42 additions & 0 deletions server/handler/conf_domain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package handler

import (
"net/url"

"github.com/ArtalkJS/Artalk/internal/core"
"github.com/ArtalkJS/Artalk/server/common"
"github.com/ArtalkJS/Artalk/server/middleware"
"github.com/gofiber/fiber/v2"
)

type ResponseConfDomain struct {
Origin string `json:"origin"` // The origin of the domain
IsTrusted bool `json:"is_trusted"` // Is the domain trusted
}

// @Id GetDomain
// @Summary Get Domain Info
// @Description Get Domain Info
// @Tags System
// @Produce json
// @Param url query string false "Domain URL"
// @Success 200 {object} ResponseConfDomain
// @Router /conf/domain [get]
func ConfDomain(app *core.App, router fiber.Router) {
router.Get("/conf/domain", func(c *fiber.Ctx) error {
domainURL := c.Query("url")
if domainURL == "" {
domainURL = c.Get("Origin")
}
u, err := url.Parse(domainURL)
if err != nil {
return common.RespError(c, 400, "Invalid URL")
}
origin := u.Scheme + "://" + u.Host
isTrusted := middleware.CheckOriginTrusted(app, origin)
return common.RespData(c, ResponseConfDomain{
IsTrusted: isTrusted,
Origin: origin,
})
})
}
4 changes: 2 additions & 2 deletions server/middleware/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ func getCorsAllowOrigins(app *core.App) []string {
return allowOrigins
}

func isOriginAllowed(app *core.App, origin string) bool {
func CheckOriginTrusted(app *core.App, origin string) bool {
return slices.Contains(getCorsAllowOrigins(app), origin)
}

func CorsMiddleware(app *core.App) func(*fiber.Ctx) error {
return cors.New(cors.Config{
AllowOriginsFunc: func(origin string) bool {
return isOriginAllowed(app, origin)
return CheckOriginTrusted(app, origin)
},
})
}
1 change: 1 addition & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func Serve(app *core.App) (*fiber.App, error) {

h.Conf(app, api)
h.Version(app, api)
h.ConfDomain(app, api)

// captcha
h.Captcha(app, api)
Expand Down
Loading

0 comments on commit cf20946

Please sign in to comment.