mirror of
https://activitypub.software/TransFem-org/Sharkey.git
synced 2024-12-15 13:25:44 +01:00
Refactoring
This commit is contained in:
parent
86858586cf
commit
bbfac657fb
2 changed files with 23 additions and 17 deletions
|
@ -1,6 +1,6 @@
|
||||||
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{ default as User, IUser } from './models/user';
|
||||||
import AccessToken from './models/access-token';
|
import AccessToken from './models/access-token';
|
||||||
import isNativeToken from './common/is-native-token';
|
import isNativeToken from './common/is-native-token';
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@ export interface IAuthContext {
|
||||||
/**
|
/**
|
||||||
* Authenticated user
|
* Authenticated user
|
||||||
*/
|
*/
|
||||||
user: any;
|
user: IUser;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Weather if the request is via the User-Native Token or not
|
* Weather if the request is via the User-Native Token or not
|
||||||
|
@ -25,11 +25,15 @@ export default (req: express.Request) => new Promise<IAuthContext>(async (resolv
|
||||||
const token = req.body['i'] as string;
|
const token = req.body['i'] as string;
|
||||||
|
|
||||||
if (token == null) {
|
if (token == null) {
|
||||||
return resolve({ app: null, user: null, isSecure: false });
|
return resolve({
|
||||||
|
app: null,
|
||||||
|
user: null,
|
||||||
|
isSecure: false
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isNativeToken(token)) {
|
if (isNativeToken(token)) {
|
||||||
const user = await User
|
const user: IUser = await User
|
||||||
.findOne({ token: token });
|
.findOne({ token: token });
|
||||||
|
|
||||||
if (user === null) {
|
if (user === null) {
|
||||||
|
@ -56,6 +60,10 @@ export default (req: express.Request) => new Promise<IAuthContext>(async (resolv
|
||||||
const user = await User
|
const user = await User
|
||||||
.findOne({ _id: accessToken.user_id });
|
.findOne({ _id: accessToken.user_id });
|
||||||
|
|
||||||
return resolve({ app: app, user: user, isSecure: false });
|
return resolve({
|
||||||
|
app: app,
|
||||||
|
user: user,
|
||||||
|
isSecure: false
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -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 config from '../conf';
|
import config from '../conf';
|
||||||
import User from './models/user';
|
import { default as User, IUser } from './models/user';
|
||||||
import AccessToken from './models/access-token';
|
import AccessToken from './models/access-token';
|
||||||
import isNativeToken from './common/is-native-token';
|
import isNativeToken from './common/is-native-token';
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@ module.exports = (server: http.Server) => {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const user = await authenticate(connection, request.resourceURL.query.i);
|
const user = await authenticate(request.resourceURL.query.i);
|
||||||
|
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
connection.send('authentication-failed');
|
connection.send('authentication-failed');
|
||||||
|
@ -56,7 +56,11 @@ module.exports = (server: http.Server) => {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
function authenticate(connection: websocket.connection, token: string): Promise<any> {
|
/**
|
||||||
|
* 接続してきたユーザーを取得します
|
||||||
|
* @param token 送信されてきたトークン
|
||||||
|
*/
|
||||||
|
function authenticate(token: string): Promise<IUser> {
|
||||||
if (token == null) {
|
if (token == null) {
|
||||||
return Promise.resolve(null);
|
return Promise.resolve(null);
|
||||||
}
|
}
|
||||||
|
@ -64,8 +68,7 @@ function authenticate(connection: websocket.connection, token: string): Promise<
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
if (isNativeToken(token)) {
|
if (isNativeToken(token)) {
|
||||||
// Fetch user
|
// Fetch user
|
||||||
// SELECT _id
|
const user: IUser = await User
|
||||||
const user = await User
|
|
||||||
.findOne({
|
.findOne({
|
||||||
token: token
|
token: token
|
||||||
});
|
});
|
||||||
|
@ -81,13 +84,8 @@ function authenticate(connection: websocket.connection, token: string): Promise<
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch user
|
// Fetch user
|
||||||
// SELECT _id
|
const user: IUser = await User
|
||||||
const user = await User
|
.findOne({ _id: accessToken.user_id });
|
||||||
.findOne({ _id: accessToken.user_id }, {
|
|
||||||
fields: {
|
|
||||||
_id: true
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
resolve(user);
|
resolve(user);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue