Skip to content

Commit 5e5f683

Browse files
authored
Merge pull request #145 from ChayanDass/refresh-token
Refresh token functionality for password login Tested-by: [email protected] Reviewed-by: [email protected]
2 parents cd4c983 + 2e432a4 commit 5e5f683

File tree

16 files changed

+516
-53
lines changed

16 files changed

+516
-53
lines changed

cmd/laas/docs/docs.go

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -844,12 +844,7 @@ const docTemplate = `{
844844
"200": {
845845
"description": "JWT token",
846846
"schema": {
847-
"type": "object",
848-
"properties": {
849-
"token": {
850-
"type": "string"
851-
}
852-
}
847+
"$ref": "#/definitions/models.TokenResonse"
853848
}
854849
},
855850
"401": {
@@ -2045,6 +2040,47 @@ const docTemplate = `{
20452040
}
20462041
}
20472042
},
2043+
"/refresh-token": {
2044+
"post": {
2045+
"description": "verify refresh token and get new access token",
2046+
"consumes": [
2047+
"application/json"
2048+
],
2049+
"produces": [
2050+
"application/json"
2051+
],
2052+
"tags": [
2053+
"Users"
2054+
],
2055+
"summary": "Verify refresh token",
2056+
"operationId": "RefreshToken",
2057+
"parameters": [
2058+
{
2059+
"description": "Refresh token payload",
2060+
"name": "user",
2061+
"in": "body",
2062+
"required": true,
2063+
"schema": {
2064+
"$ref": "#/definitions/models.RefreshToken"
2065+
}
2066+
}
2067+
],
2068+
"responses": {
2069+
"200": {
2070+
"description": " JWT token",
2071+
"schema": {
2072+
"$ref": "#/definitions/models.TokenResonse"
2073+
}
2074+
},
2075+
"401": {
2076+
"description": "Invalid or expired refresh token",
2077+
"schema": {
2078+
"$ref": "#/definitions/models.LicenseError"
2079+
}
2080+
}
2081+
}
2082+
}
2083+
},
20482084
"/search": {
20492085
"post": {
20502086
"security": [
@@ -3467,6 +3503,15 @@ const docTemplate = `{
34673503
}
34683504
}
34693505
},
3506+
"models.RefreshToken": {
3507+
"type": "object",
3508+
"properties": {
3509+
"refresh_token": {
3510+
"type": "string",
3511+
"example": "your_refresh_token_here"
3512+
}
3513+
}
3514+
},
34703515
"models.RiskLicenseCount": {
34713516
"type": "object",
34723517
"properties": {
@@ -3573,6 +3618,35 @@ const docTemplate = `{
35733618
}
35743619
}
35753620
},
3621+
"models.TokenResonse": {
3622+
"type": "object",
3623+
"properties": {
3624+
"data": {
3625+
"$ref": "#/definitions/models.Tokens"
3626+
},
3627+
"meta": {},
3628+
"status": {
3629+
"type": "integer"
3630+
}
3631+
}
3632+
},
3633+
"models.Tokens": {
3634+
"type": "object",
3635+
"properties": {
3636+
"access_token": {
3637+
"type": "string",
3638+
"example": "your_access_token_here"
3639+
},
3640+
"expires_in": {
3641+
"type": "integer",
3642+
"example": 3600
3643+
},
3644+
"refresh_token": {
3645+
"type": "string",
3646+
"example": "your_refresh_token_here"
3647+
}
3648+
}
3649+
},
35763650
"models.User": {
35773651
"type": "object",
35783652
"properties": {

cmd/laas/docs/swagger.json

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -837,12 +837,7 @@
837837
"200": {
838838
"description": "JWT token",
839839
"schema": {
840-
"type": "object",
841-
"properties": {
842-
"token": {
843-
"type": "string"
844-
}
845-
}
840+
"$ref": "#/definitions/models.TokenResonse"
846841
}
847842
},
848843
"401": {
@@ -2038,6 +2033,47 @@
20382033
}
20392034
}
20402035
},
2036+
"/refresh-token": {
2037+
"post": {
2038+
"description": "verify refresh token and get new access token",
2039+
"consumes": [
2040+
"application/json"
2041+
],
2042+
"produces": [
2043+
"application/json"
2044+
],
2045+
"tags": [
2046+
"Users"
2047+
],
2048+
"summary": "Verify refresh token",
2049+
"operationId": "RefreshToken",
2050+
"parameters": [
2051+
{
2052+
"description": "Refresh token payload",
2053+
"name": "user",
2054+
"in": "body",
2055+
"required": true,
2056+
"schema": {
2057+
"$ref": "#/definitions/models.RefreshToken"
2058+
}
2059+
}
2060+
],
2061+
"responses": {
2062+
"200": {
2063+
"description": " JWT token",
2064+
"schema": {
2065+
"$ref": "#/definitions/models.TokenResonse"
2066+
}
2067+
},
2068+
"401": {
2069+
"description": "Invalid or expired refresh token",
2070+
"schema": {
2071+
"$ref": "#/definitions/models.LicenseError"
2072+
}
2073+
}
2074+
}
2075+
}
2076+
},
20412077
"/search": {
20422078
"post": {
20432079
"security": [
@@ -3460,6 +3496,15 @@
34603496
}
34613497
}
34623498
},
3499+
"models.RefreshToken": {
3500+
"type": "object",
3501+
"properties": {
3502+
"refresh_token": {
3503+
"type": "string",
3504+
"example": "your_refresh_token_here"
3505+
}
3506+
}
3507+
},
34633508
"models.RiskLicenseCount": {
34643509
"type": "object",
34653510
"properties": {
@@ -3566,6 +3611,35 @@
35663611
}
35673612
}
35683613
},
3614+
"models.TokenResonse": {
3615+
"type": "object",
3616+
"properties": {
3617+
"data": {
3618+
"$ref": "#/definitions/models.Tokens"
3619+
},
3620+
"meta": {},
3621+
"status": {
3622+
"type": "integer"
3623+
}
3624+
}
3625+
},
3626+
"models.Tokens": {
3627+
"type": "object",
3628+
"properties": {
3629+
"access_token": {
3630+
"type": "string",
3631+
"example": "your_access_token_here"
3632+
},
3633+
"expires_in": {
3634+
"type": "integer",
3635+
"example": 3600
3636+
},
3637+
"refresh_token": {
3638+
"type": "string",
3639+
"example": "your_refresh_token_here"
3640+
}
3641+
}
3642+
},
35693643
"models.User": {
35703644
"type": "object",
35713645
"properties": {

cmd/laas/docs/swagger.yaml

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,12 @@ definitions:
694694
user_password:
695695
type: string
696696
type: object
697+
models.RefreshToken:
698+
properties:
699+
refresh_token:
700+
example: your_refresh_token_here
701+
type: string
702+
type: object
697703
models.RiskLicenseCount:
698704
properties:
699705
count:
@@ -767,6 +773,26 @@ definitions:
767773
example: 200
768774
type: integer
769775
type: object
776+
models.TokenResonse:
777+
properties:
778+
data:
779+
$ref: '#/definitions/models.Tokens'
780+
meta: {}
781+
status:
782+
type: integer
783+
type: object
784+
models.Tokens:
785+
properties:
786+
access_token:
787+
example: your_access_token_here
788+
type: string
789+
expires_in:
790+
example: 3600
791+
type: integer
792+
refresh_token:
793+
example: your_refresh_token_here
794+
type: string
795+
type: object
770796
models.User:
771797
properties:
772798
display_name:
@@ -1400,10 +1426,7 @@ paths:
14001426
"200":
14011427
description: JWT token
14021428
schema:
1403-
properties:
1404-
token:
1405-
type: string
1406-
type: object
1429+
$ref: '#/definitions/models.TokenResonse'
14071430
"401":
14081431
description: Incorrect username or password
14091432
schema:
@@ -2177,6 +2200,33 @@ paths:
21772200
summary: Adds a new oidc client
21782201
tags:
21792202
- OIDC Clients
2203+
/refresh-token:
2204+
post:
2205+
consumes:
2206+
- application/json
2207+
description: verify refresh token and get new access token
2208+
operationId: RefreshToken
2209+
parameters:
2210+
- description: Refresh token payload
2211+
in: body
2212+
name: user
2213+
required: true
2214+
schema:
2215+
$ref: '#/definitions/models.RefreshToken'
2216+
produces:
2217+
- application/json
2218+
responses:
2219+
"200":
2220+
description: ' JWT token'
2221+
schema:
2222+
$ref: '#/definitions/models.TokenResonse'
2223+
"401":
2224+
description: Invalid or expired refresh token
2225+
schema:
2226+
$ref: '#/definitions/models.LicenseError'
2227+
summary: Verify refresh token
2228+
tags:
2229+
- Users
21802230
/search:
21812231
post:
21822232
consumes:

cmd/laas/main.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-FileCopyrightText: 2023 Siemens AG
33
// SPDX-FileContributor: Gaurav Mishra <[email protected]>
44
// SPDX-FileContributor: Dearsh Oberoi <[email protected]>
5+
// SPDX-FileContributor: 2025 Chayan Das <[email protected]>
56
//
67
// SPDX-License-Identifier: GPL-2.0-only
78

@@ -16,12 +17,14 @@ import (
1617
"github.com/joho/godotenv"
1718
"github.com/lestrrat-go/httprc/v3"
1819
"github.com/lestrrat-go/jwx/v3/jwk"
20+
"go.uber.org/zap"
1921

2022
_ "github.com/dave/jennifer/jen"
2123
_ "github.com/fossology/LicenseDb/cmd/laas/docs"
2224
"github.com/fossology/LicenseDb/pkg/api"
2325
"github.com/fossology/LicenseDb/pkg/auth"
2426
"github.com/fossology/LicenseDb/pkg/db"
27+
logger "github.com/fossology/LicenseDb/pkg/log"
2528
"github.com/fossology/LicenseDb/pkg/utils"
2629
"github.com/fossology/LicenseDb/pkg/validations"
2730
)
@@ -42,18 +45,19 @@ func main() {
4245

4346
flag.Parse()
4447

45-
if os.Getenv("TOKEN_HOUR_LIFESPAN") == "" || os.Getenv("API_SECRET") == "" || os.Getenv("DEFAULT_ISSUER") == "" {
46-
log.Fatal("Mandatory environment variables not configured")
48+
if os.Getenv("TOKEN_HOUR_LIFESPAN") == "" || os.Getenv("API_SECRET") == "" || os.Getenv("DEFAULT_ISSUER") == "" ||
49+
os.Getenv("REFRESH_TOKEN_HOUR_LIFESPAN") == "" || os.Getenv("REFRESH_TOKEN_SECRET") == "" {
50+
logger.LogFatal("Mandatory environment variables not configured")
4751
}
4852

4953
if os.Getenv("JWKS_URI") != "" {
5054
cache, err := jwk.NewCache(context.Background(), httprc.NewClient())
5155
if err != nil {
52-
log.Fatalf("Failed to create a jwk.Cache from the oidc provider's URL: %s", err)
56+
logger.LogFatal("Failed to create a jwk.Cache from the oidc provider's URL:", zap.Error(err))
5357
}
5458

5559
if err := cache.Register(context.Background(), os.Getenv("JWKS_URI")); err != nil {
56-
log.Fatalf("Failed to create a jwk.Cache from the oidc provider's URL: %s", err)
60+
logger.LogFatal("Failed to create a jwk.Cache from the oidc provider's URL:", zap.Error(err))
5761
}
5862

5963
auth.Jwks = cache
@@ -78,6 +82,6 @@ func main() {
7882
r := api.Router()
7983

8084
if err := r.Run(); err != nil {
81-
log.Fatalf("Error while running the server: %v", err)
85+
logger.LogFatal("Error while running the server:", zap.Error(err))
8286
}
8387
}

configs/.env.dev.example

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
# SPDX-License-Identifier: GPL-2.0-only
22
# SPDX-FileCopyrightText: FOSSology contributors
3+
#SPDX-FileContributor: 2025 Chayan Das <[email protected]>
34

4-
# How long the token can be valid
5-
TOKEN_HOUR_LIFESPAN=24
5+
# How long the token can be valid(1 hour )
6+
TOKEN_HOUR_LIFESPAN=1
67
# Secret key to sign tokens (openssl rand -hex 32)
78
API_SECRET=some-random-string
9+
10+
# refresh token information(30 days)
11+
REFRESH_TOKEN_HOUR_LIFESPAN=720
12+
REFRESH_TOKEN_SECRET=some-other-random-string
13+
14+
815
READ_API_AUTHENTICATION_ENABLED=false
916

1017
PORT=8080

0 commit comments

Comments
 (0)