Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
MetaGer Keymanager
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
open-source
MetaGer Keymanager
Commits
ec0581c7
Commit
ec0581c7
authored
2 years ago
by
Dominik Hebeler
Browse files
Options
Downloads
Patches
Plain Diff
metager pass codes can only be redeemed once
parent
73c17a2d
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
pass/app/Key.js
+0
-1
0 additions, 1 deletion
pass/app/Key.js
pass/resources/js/checkout.js
+12
-1
12 additions, 1 deletion
pass/resources/js/checkout.js
pass/routes/redeem.js
+40
-1
40 additions, 1 deletion
pass/routes/redeem.js
with
52 additions
and
3 deletions
pass/app/Key.js
+
0
−
1
View file @
ec0581c7
...
...
@@ -70,7 +70,6 @@ class Key {
let
dayjs
=
require
(
"
dayjs
"
);
let
expiration
=
dayjs
().
add
(
Key
.
EXPIRATION_AFTER_CHARGE_DAYS
,
"
day
"
);
redis_client
.
pipeline
().
incrby
(
Key
.
DATABASE_PREFIX
+
key
,
amount
).
expireat
(
Key
.
DATABASE_PREFIX
+
key
,
expiration
.
unix
()).
exec
().
then
(
result
=>
{
console
.
log
(
result
);
resolve
({
status
:
"
SUCCESS
"
,
metager_pass_key
:
{
...
...
This diff is collapsed.
Click to expand it.
pass/resources/js/checkout.js
+
12
−
1
View file @
ec0581c7
...
...
@@ -176,7 +176,13 @@ function four_finish_purchase() {
// Make Redeem Button Work
let
redeem_button
=
document
.
getElementById
(
"
recharge-key-button
"
);
redeem_button
.
addEventListener
(
"
pointerdown
"
,
()
=>
{
redeem_button
.
addEventListener
(
"
pointerdown
"
,
e
=>
{
if
(
e
.
target
.
disabled
)
{
return
;
}
else
{
e
.
target
.
disabled
=
true
;
document
.
getElementById
(
"
metager-pass-key
"
).
disabled
=
true
;
}
let
post_data
=
{
...
redeem_data
};
post_data
.
metager_pass_key
=
document
.
getElementById
(
"
metager-pass-key
"
).
value
;
fetch
(
"
/redeem
"
,
{
...
...
@@ -187,7 +193,12 @@ function four_finish_purchase() {
body
:
JSON
.
stringify
(
post_data
),
}).
then
(
response
=>
response
.
json
())
.
then
(
response
=>
{
console
.
log
(
response
);
}).
catch
(
reason
=>
{
e
.
target
.
disabled
=
false
;
document
.
getElementById
(
"
metager-pass-key
"
).
disabled
=
false
;
console
.
error
(
reason
);
});
});
}
This diff is collapsed.
Click to expand it.
pass/routes/redeem.js
+
40
−
1
View file @
ec0581c7
...
...
@@ -8,6 +8,9 @@ const Crypto = require("../app/Crypto");
var
customParseFormat
=
require
(
"
dayjs/plugin/customParseFormat
"
);
const
Key
=
require
(
"
../app/Key
"
);
const
Order
=
require
(
"
../app/Order
"
);
const
path
=
require
(
'
path
'
);
const
fs
=
require
(
'
fs
'
);
const
readline
=
require
(
'
readline
'
);
dayjs
.
extend
(
customParseFormat
);
/**
...
...
@@ -47,7 +50,7 @@ router.use(
);
/* Recharge a MetaGer-Pass Key */
router
.
post
(
"
/
"
,
body
(
"
metager_pass_key
"
).
isWhitelisted
(
Key
.
KEY_CHARSET
).
isLength
({
min
:
6
,
max
:
20
}),
(
req
,
res
,
next
)
=>
{
router
.
post
(
"
/
"
,
body
(
"
metager_pass_key
"
).
isWhitelisted
(
Key
.
KEY_CHARSET
).
isLength
({
min
:
6
,
max
:
20
}),
async
(
req
,
res
,
next
)
=>
{
const
errors
=
validationResult
(
req
);
if
(
!
errors
.
isEmpty
())
{
return
res
.
status
(
400
).
json
({
errors
:
errors
.
array
()
});
...
...
@@ -74,6 +77,37 @@ router.post("/", body("metager_pass_key").isWhitelisted(Key.KEY_CHARSET).isLengt
charge_amount
+=
Order
.
PACKET_SIZE
;
}
// Check one or more of the codes was already used to redeem a MetaGer-Pass Key
let
order_month
=
dayjs
(
req
.
body
.
generation_month
);
let
redeem_file_path
=
path
.
join
(
config
.
get
(
"
storage.data_path
"
),
process
.
env
.
NODE_ENV
,
order_month
.
format
(
"
YYYY
"
),
order_month
.
format
(
"
MM
"
),
"
redeemed.json
"
);
if
(
!
fs
.
existsSync
(
path
.
dirname
(
redeem_file_path
)))
{
fs
.
mkdirSync
(
path
.
dirname
(
redeem_file_path
),
{
recursive
:
true
});
}
if
(
fs
.
existsSync
(
redeem_file_path
))
{
let
rl
=
readline
.
createInterface
({
input
:
fs
.
createReadStream
(
redeem_file_path
),
output
:
process
.
stdout
,
terminal
:
false
});
for
await
(
const
line
of
rl
)
{
for
(
let
i
=
0
;
i
<
req
.
body
.
metager_pass_codes
.
length
;
i
++
)
{
if
(
req
.
body
.
metager_pass_codes
[
i
].
code
===
line
.
trim
())
{
return
res
.
status
(
400
).
json
({
status
:
"
FAILED
"
,
msg
:
[
"
One or more of the provided MetaGer-Pass Codes are already redeemed.
"
]
});
}
}
}
}
let
recharge_tries
=
0
;
redis_client
.
mget
(
key_recharge_cache_keys
).
then
(
async
response
=>
{
for
(
let
i
=
0
;
i
<
response
.
length
;
i
++
)
{
...
...
@@ -100,6 +134,11 @@ router.post("/", body("metager_pass_key").isWhitelisted(Key.KEY_CHARSET).isLengt
await
redis_pipeline
.
exec
();
Key
.
CHARGE_EXISTING_KEY
(
req
.
body
.
metager_pass_key
,
charge_amount
).
then
(
result
=>
{
// Key is charged store the redeem codes into filesystem so they can only be used once
for
(
let
i
=
0
;
i
<
req
.
body
.
metager_pass_codes
.
length
;
i
++
)
{
fs
.
appendFileSync
(
redeem_file_path
,
req
.
body
.
metager_pass_codes
[
i
].
code
+
"
\n
"
);
}
res
.
json
(
result
);
}).
catch
(
reason
=>
{
res
.
status
(
400
).
json
({
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment