Commit 4558e18e authored by Krit Punpreuk's avatar Krit Punpreuk

add to git repo

parents
Pipeline #10 canceled with stages
const jwt = require('jsonwebtoken');
const express = require('express');
const bodyParser = require('body-parser')
const app = express();
const noKey = "nokey";
const secret = (process.env.BIGSTREAM_SIGNATURE) ? process.env.BIGSTREAM_SIGNATURE : noKey;
app.set('view engine', 'ejs')
app.set('views', './public')
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}))
app.use(bodyParser.json())
app.use(express.static("public"));
app.get('/', function(req, res) {
// res.status(err.status);
res.render('index', { token: '', name: '' })
});
app.get('/', (req, res) => {
res.render('index', { token: '', name: '' })
})
app.post('/', function(req, res) {
//console.log(req.body);
res.render('index', { token: generateToken(req.body.name_field), name: req.body.name_field });
})
// app.get('/', function(req, res) {
// res.sendFile("index.html")
// })
// app.post('/', function(req, res) {
// //console.log(req.body);
// res.send(generateToken(req.body.name_field));
// })
app.listen(3000, () => console.log('App listening on port 3000!'))
function generateToken(name) {
var templete = {
"vo": "igrid",
"acl": [{
"accept": true,
"resource": "igrid.*"
}],
"iat": Math.floor(Date.now() / 1000)
};
var expire = { "expiresIn": '0.5y' };
var regex_name = /^[A-Za-z0-9]+$/;
var check = regex_name.test(name);
if (!check ) {
return ("Please, enter only a-Z or 0-1 only.")
}
if (name.toLowerCase() == "bigstream" || name.toLowerCase() == "example") return ("Cannot use this name");
var username = name
templete.vo = username;
templete.acl[0].resource = username + ".*";
var token = jwt.sign(templete, secret, expire);
// var body = "<body><div>You token is <blockquote>" + token + "</blockquote></div><div>" + JSON.stringify(jwt.verify(token, secret), true, 4) + "</div></body>"
console.log("secret: "+secret+"\n");
// //console.log("before decode: "+JSON.stringify(templete)+"\n");
// console.log(body);
// var web = "<!DOCTYPE html><html lang='en'><center>" + body + "</center></html>"
return token;
}
This diff is collapsed.
{
"name": "bigstream-token-generator",
"version": "1.0.0",
"description": "bigstream token generator service",
"main": "index.js",
"scripts": {
"test": "node index.js"
},
"author": "Krit P.",
"license": "ISC",
"dependencies": {
"body-parser": "^1.18.3",
"ejs": "^2.6.1",
"express": "^4.16.3",
"jsonwebtoken": "^8.3.0"
}
}
<style>
.result {
background-color: #f1f1f1;
padding: 0.01em 16px;
margin: 20px 0;
box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.16), 0 2px 10px 0 rgba(0, 0, 0, 0.12)!important;
}
.token {
width: auto;
background-color: #fff;
padding: 8px 12px;
border-left: 4px solid #4CAF50;
word-wrap: break-word;
}
</style>
<div class="result">
<p style="text-align: left">Your token (
<%=name%> )</p>
<div class="token">
<%= token%>
</div>
<form action="./" method="get">
<input class="button blue send" type="submit" value="Reset">
</form>
</div>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Token generator</title>
</head>
<style>
@import url('https://fonts.googleapis.com/css?family=Prompt:300,400,500');
html {
width: 100%;
text-align: center;
}
body {
font-family: Prompt;
display: inline-block;
vertical-align: middle;
max-width: 38rem;
}
.logo {
margin-top: 160px;
height: 65px;
}
#subhead {
font-size: 20px;
color: #777777;
/* margin-top: 25px; */
}
.container {
/* display: flex;
flex-direction: column;
height: 100%;
margin: 0 auto;
position: relative;
z-index: 1; */
}
.button {
background-color: #4CAF50;
/* Green */
border: none;
border-radius: 3px;
color: white;
padding: 12px 50px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 30px 2px;
cursor: pointer;
-webkit-transition-duration: 0.4s;
/* Safari */
transition-duration: 0.4s;
font-family: Prompt;
font-weight: 300;
}
.blue {
background-color: #3f51b5;
}
.send {
box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
#input-style {
border: none;
border-radius: 2px;
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.16), 0 0 0 1px rgba(0, 0, 0, 0.08);
height: 44px;
outline: none;
transition: box-shadow 200ms cubic-bezier(0.4, 0.0, 0.2, 1);
width: 620px;
}
#input-style>input {
background: transparent;
border: none;
bottom: 0;
box-sizing: border-box;
left: 0;
margin: 0;
outline: none;
padding: 0 12px;
top: 2px;
width: 100%;
line-height: 45px;
font-size: 20px;
font-family: Prompt;
font-weight: 300;
color: #555;
}
</style>
<body>
<div class="container">
<img class="logo" src="images/bigstream.png">
<p id="subhead">Token generator</p>
<% if(token == ""){%>
<form id="addUserForm" action="./" method="post">
<!-- <label for="name">Enter name: </label> -->
<div id="input-style">
<input id="name" type="text" autocomplete="off" placeholder="Example: user01" required="required" pattern="^[A-Za-z0-9]+$" title="A-Za-z0-9 without white space." name="name_field">
</div>
<input class="button send" type="submit" value="Submit">
</form>
<%} else{%>
<%- include('token')%>
<%}%>
</div>
</body>
</html>
<style>
.result {
background-color: #f1f1f1;
padding: 0.01em 16px;
margin: 20px 0;
box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.16), 0 2px 10px 0 rgba(0, 0, 0, 0.12)!important;
}
.token {
width: auto;
background-color: #fff;
padding: 8px 12px;
border-left: 4px solid #4CAF50;
word-wrap: break-word;
}
</style>
<div class="result">
<p style="text-align: left">Your token (
<%=name%> )</p>
<div class="token">
<%= token%>
</div>
<form action="./" method="get">
<input class="button blue send" type="submit" value="Reset">
</form>
</div>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment