Files
misskey/src/server/api/authenticate.ts
T

41 lines
969 B
TypeScript
Raw Normal View History

2017-01-06 11:07:42 +09:00
import isNativeToken from './common/is-native-token';
2019-04-07 21:50:36 +09:00
import { User } from '../../models/entities/user';
import { App } from '../../models/entities/app';
import { Users, AccessTokens, Apps } from '../../models';
2016-12-29 07:49:51 +09:00
2019-04-13 01:43:22 +09:00
export default async (token: string): Promise<[User | null | undefined, App | null | undefined]> => {
2017-01-06 01:28:16 +09:00
if (token == null) {
2019-01-23 19:25:36 +09:00
return [null, null];
2017-01-06 01:28:16 +09:00
}
2017-01-06 11:07:42 +09:00
if (isNativeToken(token)) {
// Fetch user
2019-04-07 21:50:36 +09:00
const user = await Users
.findOne({ token });
2016-12-29 07:49:51 +09:00
2019-04-07 21:50:36 +09:00
if (user == null) {
2019-04-14 04:17:24 +09:00
throw new Error('user not found');
2016-12-29 07:49:51 +09:00
}
2019-01-23 19:25:36 +09:00
return [user, null];
2017-01-06 01:28:16 +09:00
} else {
2019-04-07 21:50:36 +09:00
const accessToken = await AccessTokens.findOne({
2017-02-08 22:49:01 +09:00
hash: token.toLowerCase()
2016-12-29 07:49:51 +09:00
});
2019-04-07 21:50:36 +09:00
if (accessToken == null) {
2019-04-14 04:17:24 +09:00
throw new Error('invalid signature');
2016-12-29 07:49:51 +09:00
}
2019-04-07 21:50:36 +09:00
const app = await Apps
.findOne(accessToken.appId);
2016-12-29 07:49:51 +09:00
2019-04-07 21:50:36 +09:00
const user = await Users
2019-04-16 01:20:28 +09:00
.findOne({
id: accessToken.userId // findOne(accessToken.userId) のように書かないのは後方互換性のため
});
2016-12-29 07:49:51 +09:00
2019-01-23 19:25:36 +09:00
return [user, app];
2016-12-29 07:49:51 +09:00
}
2019-01-23 19:25:36 +09:00
};