UNPKG

3.96 kBJavaScriptView Raw
1const mongoose = require('mongoose');
2const domain = require('../');
3
4async function main() {
5 await mongoose.connect(process.env.MONGOLAB_URI);
6
7 const projectId = 'cinerino';
8
9 const cognitoIdentityServiceProvider = new domain.AWS.CognitoIdentityServiceProvider({
10 apiVersion: 'latest',
11 region: 'ap-northeast-1',
12 credentials: new domain.AWS.Credentials({
13 accessKeyId: process.env.AWS_ACCESS_KEY_ID,
14 secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
15 })
16 });
17
18 const userPoolId = '';
19 const name = '';
20
21 // 全スコープを取得
22 const resourceServer = await new Promise((resolve, reject) => {
23 cognitoIdentityServiceProvider.describeResourceServer(
24 {
25 UserPoolId: userPoolId,
26 Identifier: 'https://api-dot-cinerino.appspot.com',
27 },
28 (err, data) => {
29 if (err instanceof Error) {
30 reject(err);
31 } else {
32 if (data.ResourceServer === undefined) {
33 reject(new cinerino.factory.errors.NotFound('ResourceServer'));
34 } else {
35 resolve(data.ResourceServer);
36 }
37 }
38 }
39 );
40 });
41
42 const allowedOAuthScopes = resourceServer.Scopes.map((scope) => `${resourceServer.Identifier}/${scope.ScopeName}`);
43
44 let callbackURLs;
45 let logoutURLs;
46 const allowedOAuthFlow = 'client_credentials';
47 // const allowedOAuthFlow = 'code';
48 if (allowedOAuthFlow === 'code') {
49 callbackURLs = ['https://localhost/signIn'];
50 logoutURLs = ['https://localhost/signOut'];
51 allowedOAuthScopes.push(...['phone', 'email', 'openid', 'aws.cognito.signin.user.admin', 'profile']);
52 }
53
54 // Cognitoでアプリケーションクライアント作成
55 const userPoolClient = await new Promise((resolve, reject) => {
56 cognitoIdentityServiceProvider.createUserPoolClient(
57 {
58 UserPoolId: userPoolId,
59 ClientName: name,
60 GenerateSecret: true,
61 // RefreshTokenValidity?: RefreshTokenValidityType;
62 // ReadAttributes?: ClientPermissionListType;
63 // WriteAttributes?: ClientPermissionListType;
64 // ExplicitAuthFlows?: ExplicitAuthFlowsListType;
65 SupportedIdentityProviders: ['COGNITO'],
66 CallbackURLs: callbackURLs,
67 LogoutURLs: logoutURLs,
68 // DefaultRedirectURI?: RedirectUrlType;
69 // AllowedOAuthFlows: ['client_credentials'],
70 AllowedOAuthFlows: [allowedOAuthFlow],
71 AllowedOAuthScopes: allowedOAuthScopes,
72 AllowedOAuthFlowsUserPoolClient: true
73 // PreventUserExistenceErrors?: PreventUserExistenceErrorTypes;
74 },
75 (err, data) => {
76 if (err instanceof Error) {
77 reject(err);
78 } else {
79 if (data.UserPoolClient === undefined) {
80 reject(new cinerino.factory.errors.NotFound('UserPool'));
81 } else {
82 resolve(data.UserPoolClient);
83 }
84 }
85 }
86 );
87 });
88 console.log('created', userPoolClient);
89
90 const applicationRepo = new domain.repository.Application(mongoose.connection);
91 const doc = await applicationRepo.applicationModel.create({
92 _id: userPoolClient.ClientId,
93 typeOf: domain.factory.creativeWorkType.WebApplication,
94 project: { typeOf: domain.factory.organizationType.Project, id: projectId },
95 name: userPoolClient.ClientName
96 });
97 console.log('created', doc.toObject());
98}
99
100main().then(() => {
101 console.log('success!');
102}).catch((error) => {
103 console.error(error);
104 process.exit(1);
105});