Skip to content

Commit aef44dd

Browse files
committed
feat!: refactor swagger API authentication (0xfurai#210)
* feat: refactor swagger API authentication - Updated security definitions in Swagger and Go files to replace BearerAuth with JwtAuth. - Simplified API key and JWT authentication across various controllers and middleware. - Removed unauthorized response handling from API documentation. - Enhanced API key management documentation for clarity. - Updated frontend SDK to reflect changes in API key and JWT authentication methods. * refactor(auth_chain): update logging level for missing authentication headers - Changed log level from Warn to Debug for missing authentication headers in AllAuth method to reduce log noise. - Minor formatting adjustment in the code for improved readability. * refactor(auth_chain): change logging levels for authentication routing - Updated log level from Info to Debug for API key and JWT authentication routing to reduce log verbosity. - Changed log level from Debug to Warn for missing authentication headers to highlight critical issues more effectively. * fix(api_key): standardize error messages for invalid API keys - Updated error messages in the ValidateKey method to use lowercase "invalid API key" for consistency. * fix(api_key): update context usage in API key validation - Changed the context parameter in the ValidateKey method call from the gin context to the request context for improved consistency and reliability in API key validation. * feat(api_key): add comprehensive tests for API key service and middleware - Introduced unit tests for the API key service, covering key creation, validation, expiration handling, and usage limits. - Added middleware tests to ensure proper authentication flow and error handling for missing or invalid API keys. - Implemented a mock service for testing middleware interactions, enhancing test coverage and reliability. * feat(api_key): add integration tests for API key service functionality - Introduced a new integration test suite for the API key service, covering key creation, validation, expiration handling, and usage limits. - Implemented tests for middleware interactions with real HTTP requests to ensure proper authentication flow. - Enhanced test coverage by validating key usage count updates and handling of expired keys.
1 parent f9ed82c commit aef44dd

23 files changed

+1777
-399
lines changed

apps/server/docs/docs.go

Lines changed: 115 additions & 58 deletions
Large diffs are not rendered by default.

apps/server/docs/swagger.json

Lines changed: 115 additions & 58 deletions
Large diffs are not rendered by default.

apps/server/docs/swagger.yaml

Lines changed: 62 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,31 +1367,27 @@ info:
13671367
paths:
13681368
/api-keys:
13691369
get:
1370-
description: Get all API keys for the authenticated user
1370+
description: Get all API keys
13711371
produces:
13721372
- application/json
13731373
responses:
13741374
"200":
13751375
description: OK
13761376
schema:
13771377
$ref: '#/definitions/utils.ApiResponse-array_api_key_APIKeyResponse'
1378-
"401":
1379-
description: Unauthorized
1380-
schema:
1381-
$ref: '#/definitions/utils.APIError'
13821378
"500":
13831379
description: Internal Server Error
13841380
schema:
13851381
$ref: '#/definitions/utils.APIError'
13861382
security:
1387-
- BearerAuth: []
1383+
- JwtAuth: []
13881384
summary: Get API keys
13891385
tags:
13901386
- api-keys
13911387
post:
13921388
consumes:
13931389
- application/json
1394-
description: Create a new API key for the authenticated user
1390+
description: Create a new API key
13951391
parameters:
13961392
- description: API key creation data
13971393
in: body
@@ -1410,16 +1406,12 @@ paths:
14101406
description: Bad Request
14111407
schema:
14121408
$ref: '#/definitions/utils.APIError'
1413-
"401":
1414-
description: Unauthorized
1415-
schema:
1416-
$ref: '#/definitions/utils.APIError'
14171409
"500":
14181410
description: Internal Server Error
14191411
schema:
14201412
$ref: '#/definitions/utils.APIError'
14211413
security:
1422-
- BearerAuth: []
1414+
- JwtAuth: []
14231415
summary: Create API key
14241416
tags:
14251417
- api-keys
@@ -1435,10 +1427,6 @@ paths:
14351427
responses:
14361428
"204":
14371429
description: No Content
1438-
"401":
1439-
description: Unauthorized
1440-
schema:
1441-
$ref: '#/definitions/utils.APIError'
14421430
"404":
14431431
description: Not Found
14441432
schema:
@@ -1448,7 +1436,7 @@ paths:
14481436
schema:
14491437
$ref: '#/definitions/utils.APIError'
14501438
security:
1451-
- BearerAuth: []
1439+
- JwtAuth: []
14521440
summary: Delete API key
14531441
tags:
14541442
- api-keys
@@ -1467,10 +1455,6 @@ paths:
14671455
description: OK
14681456
schema:
14691457
$ref: '#/definitions/utils.ApiResponse-api_key_APIKeyResponse'
1470-
"401":
1471-
description: Unauthorized
1472-
schema:
1473-
$ref: '#/definitions/utils.APIError'
14741458
"404":
14751459
description: Not Found
14761460
schema:
@@ -1480,7 +1464,7 @@ paths:
14801464
schema:
14811465
$ref: '#/definitions/utils.APIError'
14821466
security:
1483-
- BearerAuth: []
1467+
- JwtAuth: []
14841468
summary: Get API key
14851469
tags:
14861470
- api-keys
@@ -1511,10 +1495,6 @@ paths:
15111495
description: Bad Request
15121496
schema:
15131497
$ref: '#/definitions/utils.APIError'
1514-
"401":
1515-
description: Unauthorized
1516-
schema:
1517-
$ref: '#/definitions/utils.APIError'
15181498
"404":
15191499
description: Not Found
15201500
schema:
@@ -1524,7 +1504,7 @@ paths:
15241504
schema:
15251505
$ref: '#/definitions/utils.APIError'
15261506
security:
1527-
- BearerAuth: []
1507+
- JwtAuth: []
15281508
summary: Update API key
15291509
tags:
15301510
- api-keys
@@ -1567,6 +1547,8 @@ paths:
15671547
description: Internal Server Error
15681548
schema:
15691549
$ref: '#/definitions/utils.APIError'
1550+
security:
1551+
- JwtAuth: []
15701552
summary: Disable 2FA (TOTP) for user
15711553
tags:
15721554
- Auth
@@ -1596,6 +1578,8 @@ paths:
15961578
description: Internal Server Error
15971579
schema:
15981580
$ref: '#/definitions/utils.APIError'
1581+
security:
1582+
- JwtAuth: []
15991583
summary: Enable 2FA (TOTP) for user
16001584
tags:
16011585
- Auth
@@ -1625,6 +1609,8 @@ paths:
16251609
description: Internal Server Error
16261610
schema:
16271611
$ref: '#/definitions/utils.APIError'
1612+
security:
1613+
- JwtAuth: []
16281614
summary: Verify 2FA (TOTP) code for user
16291615
tags:
16301616
- Auth
@@ -1691,6 +1677,8 @@ paths:
16911677
description: Internal Server Error
16921678
schema:
16931679
$ref: '#/definitions/utils.APIError'
1680+
security:
1681+
- JwtAuth: []
16941682
summary: Update user password
16951683
tags:
16961684
- Auth
@@ -2056,7 +2044,8 @@ paths:
20562044
schema:
20572045
$ref: '#/definitions/utils.APIError'
20582046
security:
2059-
- BearerAuth: []
2047+
- JwtAuth: []
2048+
- ApiKeyAuth: []
20602049
summary: Get maintenances
20612050
tags:
20622051
- Maintenances
@@ -2086,7 +2075,8 @@ paths:
20862075
schema:
20872076
$ref: '#/definitions/utils.APIError'
20882077
security:
2089-
- BearerAuth: []
2078+
- JwtAuth: []
2079+
- ApiKeyAuth: []
20902080
summary: Create maintenance
20912081
tags:
20922082
- Maintenances
@@ -2344,7 +2334,8 @@ paths:
23442334
schema:
23452335
$ref: '#/definitions/utils.APIError'
23462336
security:
2347-
- BearerAuth: []
2337+
- JwtAuth: []
2338+
- ApiKeyAuth: []
23482339
summary: Get monitors
23492340
tags:
23502341
- Monitors
@@ -2374,7 +2365,8 @@ paths:
23742365
schema:
23752366
$ref: '#/definitions/utils.APIError'
23762367
security:
2377-
- BearerAuth: []
2368+
- JwtAuth: []
2369+
- ApiKeyAuth: []
23782370
summary: Create monitor
23792371
tags:
23802372
- Monitors
@@ -2732,7 +2724,8 @@ paths:
27322724
schema:
27332725
$ref: '#/definitions/utils.APIError'
27342726
security:
2735-
- BearerAuth: []
2727+
- JwtAuth: []
2728+
- ApiKeyAuth: []
27362729
summary: Get monitors by IDs
27372730
tags:
27382731
- Monitors
@@ -2773,7 +2766,8 @@ paths:
27732766
schema:
27742767
$ref: '#/definitions/utils.APIError'
27752768
security:
2776-
- BearerAuth: []
2769+
- JwtAuth: []
2770+
- ApiKeyAuth: []
27772771
summary: Get notification channels
27782772
tags:
27792773
- Notification channels
@@ -2803,7 +2797,8 @@ paths:
28032797
schema:
28042798
$ref: '#/definitions/utils.APIError'
28052799
security:
2806-
- BearerAuth: []
2800+
- JwtAuth: []
2801+
- ApiKeyAuth: []
28072802
summary: Create notification channel
28082803
tags:
28092804
- Notification channels
@@ -2975,7 +2970,8 @@ paths:
29752970
schema:
29762971
$ref: '#/definitions/utils.APIError'
29772972
security:
2978-
- BearerAuth: []
2973+
- JwtAuth: []
2974+
- ApiKeyAuth: []
29792975
summary: Test notification channel
29802976
tags:
29812977
- Notification channels
@@ -3016,7 +3012,8 @@ paths:
30163012
schema:
30173013
$ref: '#/definitions/utils.APIError'
30183014
security:
3019-
- BearerAuth: []
3015+
- JwtAuth: []
3016+
- ApiKeyAuth: []
30203017
summary: Get proxies
30213018
tags:
30223019
- Proxies
@@ -3046,7 +3043,8 @@ paths:
30463043
schema:
30473044
$ref: '#/definitions/utils.APIError'
30483045
security:
3049-
- BearerAuth: []
3046+
- JwtAuth: []
3047+
- ApiKeyAuth: []
30503048
summary: Create proxy
30513049
tags:
30523050
- Proxies
@@ -3215,7 +3213,8 @@ paths:
32153213
schema:
32163214
$ref: '#/definitions/utils.APIError'
32173215
security:
3218-
- BearerAuth: []
3216+
- JwtAuth: []
3217+
- ApiKeyAuth: []
32193218
summary: Delete setting by key
32203219
tags:
32213220
- Settings
@@ -3242,7 +3241,8 @@ paths:
32423241
schema:
32433242
$ref: '#/definitions/utils.APIError'
32443243
security:
3245-
- BearerAuth: []
3244+
- JwtAuth: []
3245+
- ApiKeyAuth: []
32463246
summary: Get setting by key
32473247
tags:
32483248
- Settings
@@ -3277,7 +3277,8 @@ paths:
32773277
schema:
32783278
$ref: '#/definitions/utils.APIError'
32793279
security:
3280-
- BearerAuth: []
3280+
- JwtAuth: []
3281+
- ApiKeyAuth: []
32813282
summary: Set setting by key
32823283
tags:
32833284
- Settings
@@ -3314,7 +3315,8 @@ paths:
33143315
schema:
33153316
$ref: '#/definitions/utils.APIError'
33163317
security:
3317-
- BearerAuth: []
3318+
- JwtAuth: []
3319+
- ApiKeyAuth: []
33183320
summary: Get all status pages
33193321
tags:
33203322
- Status Pages
@@ -3344,7 +3346,8 @@ paths:
33443346
schema:
33453347
$ref: '#/definitions/utils.APIError'
33463348
security:
3347-
- BearerAuth: []
3349+
- JwtAuth: []
3350+
- ApiKeyAuth: []
33483351
summary: Create a new status page
33493352
tags:
33503353
- Status Pages
@@ -3372,7 +3375,8 @@ paths:
33723375
schema:
33733376
$ref: '#/definitions/utils.APIError'
33743377
security:
3375-
- BearerAuth: []
3378+
- JwtAuth: []
3379+
- ApiKeyAuth: []
33763380
summary: Delete a status page
33773381
tags:
33783382
- Status Pages
@@ -3399,7 +3403,8 @@ paths:
33993403
schema:
34003404
$ref: '#/definitions/utils.APIError'
34013405
security:
3402-
- BearerAuth: []
3406+
- JwtAuth: []
3407+
- ApiKeyAuth: []
34033408
summary: Get a status page by ID
34043409
tags:
34053410
- Status Pages
@@ -3438,7 +3443,8 @@ paths:
34383443
schema:
34393444
$ref: '#/definitions/utils.APIError'
34403445
security:
3441-
- BearerAuth: []
3446+
- JwtAuth: []
3447+
- ApiKeyAuth: []
34423448
summary: Update a status page
34433449
tags:
34443450
- Status Pages
@@ -3583,7 +3589,8 @@ paths:
35833589
schema:
35843590
$ref: '#/definitions/utils.APIError'
35853591
security:
3586-
- BearerAuth: []
3592+
- JwtAuth: []
3593+
- ApiKeyAuth: []
35873594
summary: Get tags
35883595
tags:
35893596
- Tags
@@ -3613,7 +3620,8 @@ paths:
36133620
schema:
36143621
$ref: '#/definitions/utils.APIError'
36153622
security:
3616-
- BearerAuth: []
3623+
- JwtAuth: []
3624+
- ApiKeyAuth: []
36173625
summary: Create tag
36183626
tags:
36193627
- Tags
@@ -3774,7 +3782,13 @@ paths:
37743782
tags:
37753783
- System
37763784
securityDefinitions:
3777-
BearerAuth:
3785+
ApiKeyAuth:
3786+
description: API key authentication (pk_ prefix format)
3787+
in: header
3788+
name: X-API-Key
3789+
type: apiKey
3790+
JwtAuth:
3791+
description: JWT token authentication (Bearer token format)
37783792
in: header
37793793
name: Authorization
37803794
type: apiKey

apps/server/src/main.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,14 @@ import (
4242

4343
// @title Peekaping API
4444
// @BasePath /api/v1
45-
// @securityDefinitions.apikey BearerAuth
45+
// @securityDefinitions.apikey JwtAuth
4646
// @in header
4747
// @name Authorization
48+
// @description JWT token authentication (Bearer token format)
49+
// @securityDefinitions.apikey ApiKeyAuth
50+
// @in header
51+
// @name X-API-Key
52+
// @description API key authentication (pk_ prefix format)
4853
func main() {
4954
docs.SwaggerInfo.Version = version.Version
5055

0 commit comments

Comments
 (0)