Refactor: Rename userkey --> access-token
This commit is contained in:
parent
9458245382
commit
fef3d3f300
|
@ -1,7 +1,7 @@
|
||||||
import * as express from 'express';
|
import * as express from 'express';
|
||||||
import App from './models/app';
|
import App from './models/app';
|
||||||
import User from './models/user';
|
import User from './models/user';
|
||||||
import Userkey from './models/userkey';
|
import AccessToken from './models/access-token';
|
||||||
import isNativeToken from './common/is-native-token';
|
import isNativeToken from './common/is-native-token';
|
||||||
|
|
||||||
export interface IAuthContext {
|
export interface IAuthContext {
|
||||||
|
@ -42,19 +42,19 @@ export default (req: express.Request) => new Promise<IAuthContext>(async (resolv
|
||||||
isSecure: true
|
isSecure: true
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
const userkeyDoc = await Userkey.findOne({
|
const accessToken = await AccessToken.findOne({
|
||||||
hash: token
|
hash: token
|
||||||
});
|
});
|
||||||
|
|
||||||
if (userkeyDoc === null) {
|
if (accessToken === null) {
|
||||||
return reject('invalid userkey');
|
return reject('invalid token');
|
||||||
}
|
}
|
||||||
|
|
||||||
const app = await App
|
const app = await App
|
||||||
.findOne({ _id: userkeyDoc.app_id });
|
.findOne({ _id: accessToken.app_id });
|
||||||
|
|
||||||
const user = await User
|
const user = await User
|
||||||
.findOne({ _id: userkeyDoc.user_id });
|
.findOne({ _id: accessToken.user_id });
|
||||||
|
|
||||||
return resolve({ app: app, user: user, isSecure: false });
|
return resolve({ app: app, user: user, isSecure: false });
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import rndstr from 'rndstr';
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
import App from '../../models/app';
|
import App from '../../models/app';
|
||||||
import AuthSess from '../../models/auth-session';
|
import AuthSess from '../../models/auth-session';
|
||||||
import Userkey from '../../models/userkey';
|
import AccessToken from '../../models/access-token';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Accept
|
* Accept
|
||||||
|
@ -20,24 +20,24 @@ module.exports = (params, user) =>
|
||||||
new Promise(async (res, rej) =>
|
new Promise(async (res, rej) =>
|
||||||
{
|
{
|
||||||
// Get 'token' parameter
|
// Get 'token' parameter
|
||||||
const token = params.token;
|
const sesstoken = params.token;
|
||||||
if (token == null) {
|
if (sesstoken == null) {
|
||||||
return rej('token is required');
|
return rej('token is required');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch token
|
// Fetch token
|
||||||
const session = await AuthSess
|
const session = await AuthSess
|
||||||
.findOne({ token: token });
|
.findOne({ token: sesstoken });
|
||||||
|
|
||||||
if (session === null) {
|
if (session === null) {
|
||||||
return rej('session not found');
|
return rej('session not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate userkey
|
// Generate access token
|
||||||
const key = rndstr('a-zA-Z0-9', 32);
|
const token = rndstr('a-zA-Z0-9', 32);
|
||||||
|
|
||||||
// Fetch exist userkey
|
// Fetch exist access token
|
||||||
const exist = await Userkey.findOne({
|
const exist = await AccessToken.findOne({
|
||||||
app_id: session.app_id,
|
app_id: session.app_id,
|
||||||
user_id: user._id,
|
user_id: user._id,
|
||||||
});
|
});
|
||||||
|
@ -50,15 +50,15 @@ module.exports = (params, user) =>
|
||||||
|
|
||||||
// Generate Hash
|
// Generate Hash
|
||||||
const sha512 = crypto.createHash('sha512');
|
const sha512 = crypto.createHash('sha512');
|
||||||
sha512.update(key + app.secret);
|
sha512.update(token + app.secret);
|
||||||
const hash = sha512.digest('hex');
|
const hash = sha512.digest('hex');
|
||||||
|
|
||||||
// Insert userkey doc
|
// Insert access token doc
|
||||||
await Userkey.insert({
|
await AccessToken.insert({
|
||||||
created_at: new Date(),
|
created_at: new Date(),
|
||||||
app_id: session.app_id,
|
app_id: session.app_id,
|
||||||
user_id: user._id,
|
user_id: user._id,
|
||||||
key: key,
|
token: token,
|
||||||
hash: hash
|
hash: hash
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
*/
|
*/
|
||||||
import App from '../../../models/app';
|
import App from '../../../models/app';
|
||||||
import AuthSess from '../../../models/auth-session';
|
import AuthSess from '../../../models/auth-session';
|
||||||
import Userkey from '../../../models/userkey';
|
import AccessToken from '../../../models/access-token';
|
||||||
import serialize from '../../../serializers/user';
|
import serialize from '../../../serializers/user';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -53,8 +53,8 @@ module.exports = (params) =>
|
||||||
return rej('this session is not allowed yet');
|
return rej('this session is not allowed yet');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lookup userkey
|
// Lookup access token
|
||||||
const userkey = await Userkey.findOne({
|
const accessToken = await AccessToken.findOne({
|
||||||
app_id: app._id,
|
app_id: app._id,
|
||||||
user_id: session.user_id
|
user_id: session.user_id
|
||||||
});
|
});
|
||||||
|
@ -66,7 +66,7 @@ module.exports = (params) =>
|
||||||
|
|
||||||
// Response
|
// Response
|
||||||
res({
|
res({
|
||||||
userkey: userkey.key,
|
access_token: accessToken.token,
|
||||||
user: await serialize(session.user_id, null, {
|
user: await serialize(session.user_id, null, {
|
||||||
detail: true
|
detail: true
|
||||||
})
|
})
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
const collection = global.db.collection('access_tokens');
|
||||||
|
|
||||||
|
collection.createIndex('token');
|
||||||
|
collection.createIndex('hash');
|
||||||
|
|
||||||
|
export default collection;
|
|
@ -1,5 +0,0 @@
|
||||||
const collection = global.db.collection('userkeys');
|
|
||||||
|
|
||||||
collection.createIndex('key');
|
|
||||||
|
|
||||||
export default collection;
|
|
|
@ -7,7 +7,7 @@ import * as mongo from 'mongodb';
|
||||||
import deepcopy = require('deepcopy');
|
import deepcopy = require('deepcopy');
|
||||||
import App from '../models/app';
|
import App from '../models/app';
|
||||||
import User from '../models/user';
|
import User from '../models/user';
|
||||||
import Userkey from '../models/userkey';
|
import AccessToken from '../models/access-token';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Serialize an app
|
* Serialize an app
|
||||||
|
@ -71,7 +71,7 @@ export default (
|
||||||
|
|
||||||
if (me) {
|
if (me) {
|
||||||
// 既に連携しているか
|
// 既に連携しているか
|
||||||
const exist = await Userkey.count({
|
const exist = await AccessToken.count({
|
||||||
app_id: _app.id,
|
app_id: _app.id,
|
||||||
user_id: me,
|
user_id: me,
|
||||||
}, {
|
}, {
|
||||||
|
|
|
@ -2,7 +2,7 @@ import * as http from 'http';
|
||||||
import * as websocket from 'websocket';
|
import * as websocket from 'websocket';
|
||||||
import * as redis from 'redis';
|
import * as redis from 'redis';
|
||||||
import User from './models/user';
|
import User from './models/user';
|
||||||
import Userkey from './models/userkey';
|
import AccessToken from './models/access-token';
|
||||||
import isNativeToken from './common/is-native-token';
|
import isNativeToken from './common/is-native-token';
|
||||||
|
|
||||||
import homeStream from './stream/home';
|
import homeStream from './stream/home';
|
||||||
|
@ -63,18 +63,18 @@ function authenticate(connection: websocket.connection, token: string): Promise<
|
||||||
|
|
||||||
resolve(user);
|
resolve(user);
|
||||||
} else {
|
} else {
|
||||||
const userkey = await Userkey.findOne({
|
const accessToken = await AccessToken.findOne({
|
||||||
hash: token
|
hash: token
|
||||||
});
|
});
|
||||||
|
|
||||||
if (userkey == null) {
|
if (accessToken == null) {
|
||||||
return reject('invalid userkey');
|
return reject('invalid token');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch user
|
// Fetch user
|
||||||
// SELECT _id
|
// SELECT _id
|
||||||
const user = await User
|
const user = await User
|
||||||
.findOne({ _id: userkey.user_id }, {
|
.findOne({ _id: accessToken.user_id }, {
|
||||||
_id: true
|
_id: true
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue