Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
security-ndjs-lib
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Percy Quispe Huarcaya
security-ndjs-lib
Commits
db3ad43d
Commit
db3ad43d
authored
Nov 04, 2024
by
Percy Quispe Huarcaya
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: Adding token creator and a parser
parent
8ec73473
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
106 additions
and
0 deletions
+106
-0
osutils.js
src/util/osutils.js
+57
-0
token.js
src/util/token.js
+49
-0
No files found.
src/util/osutils.js
0 → 100644
View file @
db3ad43d
import
os
from
'os'
;
import
fs
from
'fs'
;
import
path
from
'path'
;
const
DEFAULT_UNIX
=
'/opt/dotenv/'
;
// Helper function to detect the operating system
export
const
getOperatingSystemType
=
()
=>
{
const
platform
=
os
.
platform
();
switch
(
platform
)
{
case
'darwin'
:
return
'MacOS'
;
case
'win32'
:
return
'Windows'
;
case
'linux'
:
return
'Linux'
;
default
:
throw
new
Error
(
`Unsupported platform:
${
platform
}
`
);
}
};
// Helper function for Windows-specific path check
export
const
findWindowsPath
=
(
projectName
)
=>
{
const
alphabet
=
'abcdefghijklmnopqrstuvwxyz'
.
split
(
''
);
for
(
const
letter
of
alphabet
)
{
const
winPath
=
`
${
letter
}
:/dotenv/
${
projectName
}
`
;
if
(
fs
.
existsSync
(
winPath
))
{
return
winPath
;
}
}
return
null
;
// No valid path found
};
// Main function to get the default project path
export
const
getDefaultPath
=
(
projectName
,
...
customPath
)
=>
{
const
detectedOs
=
getOperatingSystemType
();
if
(
!
projectName
)
{
throw
new
Error
(
'Project name is required.'
);
}
switch
(
detectedOs
)
{
case
'MacOS'
:
case
'Linux'
:
{
const
basePath
=
customPath
[
0
]
?
customPath
[
0
]
:
DEFAULT_UNIX
;
return
path
.
join
(
basePath
,
projectName
);
}
case
'Windows'
:
{
if
(
customPath
[
0
])
{
return
path
.
join
(
customPath
[
0
],
projectName
);
}
else
{
const
windowsPath
=
findWindowsPath
(
projectName
);
if
(
!
windowsPath
)
{
throw
new
Error
(
'No valid Windows drive found for the path.'
);
}
return
windowsPath
;
}
}
default
:
throw
new
Error
(
`Unsupported OS:
${
detectedOs
}
`
);
}
};
src/util/token.js
0 → 100644
View file @
db3ad43d
import
jwt
from
'jsonwebtoken'
;
import
{
getDefaultPath
}
from
'./osutils'
;
import
dotenv
from
'dotenv'
;
// Cargar las variables de entorno desde el archivo .env
dotenv
.
config
({
path
:
getDefaultPath
(
'global'
)
+
'/.env'
});
const
apiKey
=
process
.
env
.
API_KEY
;
const
tokenAlgorithm
=
process
.
env
.
TOKEN_ALGORITHM
;
export
const
createJWT
=
(
id
,
subject
,
...
time
)
=>
{
return
createToken
(
id
,
"oliverpqh@gmail.com"
,
subject
,
...
time
);
};
export
const
createToken
=
(
id
,
issuer
,
subject
,
...
time
)
=>
{
const
ttlMillis
=
time
[
0
]
||
Number
(
process
.
env
.
VIGENCIA_TOKEN
)
||
3600000
;
// Default to 1 hour
const
now
=
Date
.
now
();
if
(
!
apiKey
)
{
throw
new
Error
(
'API_KEY is required but not defined in environment variables.'
);
}
const
signingKey
=
Buffer
.
from
(
apiKey
,
'base64'
);
// JWT payload
const
payload
=
{
id
:
id
,
issuedAt
:
Math
.
floor
(
now
/
1000
),
subject
:
subject
,
issuer
:
issuer
};
const
options
=
{
algorithm
:
tokenAlgorithm
,
expiresIn
:
ttlMillis
>=
0
?
Math
.
floor
((
now
+
ttlMillis
)
/
1000
)
:
undefined
};
// Create and sign the JWT
const
token
=
jwt
.
sign
(
payload
,
signingKey
,
options
);
return
token
;
};
export
const
parseJWT
=
(
token
,
...
id
)
=>
{
try
{
// Ensure apiKey is defined
if
(
!
apiKey
)
{
throw
new
Error
(
'API_KEY is required but not defined in environment variables.'
);
}
const
decoded
=
jwt
.
verify
(
token
,
Buffer
.
from
(
apiKey
,
'base64'
),
{
algorithms
:
[
tokenAlgorithm
]
});
console
.
log
(
"parseJWT--------------------"
);
console
.
log
(
decoded
);
return
decoded
;
}
catch
(
err
)
{
throw
err
;
}
};
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment