Slack login
This commit is contained in:
parent
22340d8a96
commit
517d2b59b6
7 changed files with 131 additions and 4 deletions
|
@ -7,5 +7,10 @@ export default {
|
||||||
password: 'POSTGRES_PASSWORD',
|
password: 'POSTGRES_PASSWORD',
|
||||||
database: 'POSTGRES_DB',
|
database: 'POSTGRES_DB',
|
||||||
},
|
},
|
||||||
|
slackConfig: {
|
||||||
|
client_id: 'SLACK_CLIENT_ID',
|
||||||
|
client_secret: 'SLACK_CLIENT_SECRET',
|
||||||
|
signing_secret: 'SLACK_SIGNING_SECRET'
|
||||||
|
},
|
||||||
admin_token: 'ADMIN_TOKEN'
|
admin_token: 'ADMIN_TOKEN'
|
||||||
}
|
}
|
|
@ -7,4 +7,8 @@ POSTGRES_USER=
|
||||||
POSTGRES_PASSWORD=
|
POSTGRES_PASSWORD=
|
||||||
POSTGRES_DB=
|
POSTGRES_DB=
|
||||||
|
|
||||||
|
SLACK_CLIENT_ID=
|
||||||
|
SLACK_CLIENT_SECRET=
|
||||||
|
SLACK_SIGNING_SECRET=
|
||||||
|
|
||||||
ADMIN_TOKEN=
|
ADMIN_TOKEN=
|
|
@ -8,6 +8,7 @@ import { AppDataSource } from './db/data-source';
|
||||||
import AppError from './errors/appError';
|
import AppError from './errors/appError';
|
||||||
|
|
||||||
import groupRoutes from "./routes/group.routes";
|
import groupRoutes from "./routes/group.routes";
|
||||||
|
import slackRoutes from "./routes/slack.routes";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -25,6 +26,13 @@ AppDataSource.initialize()
|
||||||
|
|
||||||
app.use('/groups', groupRoutes);
|
app.use('/groups', groupRoutes);
|
||||||
|
|
||||||
|
// app.use('/slack', async (req:Request, res:Response) => {
|
||||||
|
// console.log(req)
|
||||||
|
// let code = req.query.code;
|
||||||
|
// });
|
||||||
|
|
||||||
|
app.use('/slack', slackRoutes);
|
||||||
|
|
||||||
app.all('*', (req: Request, res: Response, next: NextFunction) => {
|
app.all('*', (req: Request, res: Response, next: NextFunction) => {
|
||||||
next(new AppError(404, `Route ${req.originalUrl} not found`));
|
next(new AppError(404, `Route ${req.originalUrl} not found`));
|
||||||
});
|
});
|
||||||
|
|
74
server/controllers/slack.controller.ts
Normal file
74
server/controllers/slack.controller.ts
Normal file
|
@ -0,0 +1,74 @@
|
||||||
|
import config from 'config';
|
||||||
|
const { InstallProvider, LogLevel, FileInstallationStore } = require('@slack/oauth');
|
||||||
|
import {NextFunction, Request, Response} from 'express';
|
||||||
|
import {AppDataSource} from "../db/data-source";
|
||||||
|
import {Bridge} from "../entities/bridge.entity";
|
||||||
|
import {Group} from "../entities/group.entity";
|
||||||
|
import { randomUUID } from "crypto";
|
||||||
|
|
||||||
|
const scopes = ['bot', 'channels:write', 'chat:write:bot', 'chat:write:user', 'users.profile:read'];
|
||||||
|
const bridgeRepository = AppDataSource.getRepository(Bridge)
|
||||||
|
const groupRepository = AppDataSource.getRepository(Group)
|
||||||
|
|
||||||
|
|
||||||
|
const slackConfig = config.get<{
|
||||||
|
client_id: string,
|
||||||
|
client_secret: string,
|
||||||
|
signing_secret: string
|
||||||
|
}>('slackConfig');
|
||||||
|
|
||||||
|
const installer = new InstallProvider({
|
||||||
|
clientId: slackConfig.client_id,
|
||||||
|
clientSecret: slackConfig.client_secret,
|
||||||
|
authVersion: 'v1',
|
||||||
|
scopes,
|
||||||
|
stateSecret: randomUUID(),
|
||||||
|
installationStore: new FileInstallationStore(),
|
||||||
|
logLevel: LogLevel.DEBUG,
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
export const SlackInstallHandler = async(
|
||||||
|
req: Request,
|
||||||
|
res: Response
|
||||||
|
) => {
|
||||||
|
|
||||||
|
|
||||||
|
await installer.handleInstallPath(req, res, {}, {
|
||||||
|
scopes,
|
||||||
|
metadata: {'name':'my-slack-name','group':'MyGroup'}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export const SlackCallbackHandler = async(
|
||||||
|
req: Request,
|
||||||
|
res: Response
|
||||||
|
) => {
|
||||||
|
// using custom success and failure handlers
|
||||||
|
const callbackOptions = {
|
||||||
|
success: async (installation, installOptions, req, res) => {
|
||||||
|
console.log(installation, installOptions, req.body, req.content, req.query, req.params)
|
||||||
|
console.log(installation.team.id, installation.team.name, installation.bot.token);
|
||||||
|
let bridge = await bridgeRepository.create({
|
||||||
|
'Protocol': 'slack',
|
||||||
|
'Label': installation.metadata.name,
|
||||||
|
'team_id': installation.team.id,
|
||||||
|
'team_name': installation.team.name,
|
||||||
|
'Token': installation.bot.token
|
||||||
|
});
|
||||||
|
let result = await bridgeRepository.save(bridge);
|
||||||
|
|
||||||
|
|
||||||
|
res.send(result);
|
||||||
|
|
||||||
|
},
|
||||||
|
failure: (error, installOptions , req, res) => {
|
||||||
|
res.send('failure');
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
await installer.handleCallback(req, res, callbackOptions);
|
||||||
|
|
||||||
|
}
|
|
@ -20,6 +20,18 @@ export class Bridge extends Model {
|
||||||
@Column()
|
@Column()
|
||||||
Label: string;
|
Label: string;
|
||||||
|
|
||||||
|
// The ID of the team
|
||||||
|
@Column({nullable:true})
|
||||||
|
team_id: string;
|
||||||
|
|
||||||
|
@Column({nullable:true})
|
||||||
|
team_name: string;
|
||||||
|
|
||||||
|
@Column({
|
||||||
|
unique: true
|
||||||
|
})
|
||||||
|
Token: string;
|
||||||
|
|
||||||
@Column({
|
@Column({
|
||||||
default: true
|
default: true
|
||||||
})
|
})
|
||||||
|
@ -31,10 +43,7 @@ export class Bridge extends Model {
|
||||||
})
|
})
|
||||||
RemoteNickFormat: string;
|
RemoteNickFormat: string;
|
||||||
|
|
||||||
@Column({
|
|
||||||
unique: true
|
|
||||||
})
|
|
||||||
Token: string;
|
|
||||||
|
|
||||||
@OneToMany(() => Channel, (channel) => channel.bridge)
|
@OneToMany(() => Channel, (channel) => channel.bridge)
|
||||||
channels: Channel[]
|
channels: Channel[]
|
||||||
|
|
16
server/routes/slack.routes.ts
Normal file
16
server/routes/slack.routes.ts
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
import express from 'express';
|
||||||
|
|
||||||
|
import {
|
||||||
|
SlackInstallHandler,
|
||||||
|
SlackCallbackHandler
|
||||||
|
} from '../controllers/slack.controller'
|
||||||
|
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
router.route('/install')
|
||||||
|
.get(SlackInstallHandler)
|
||||||
|
|
||||||
|
router.route('/oauth_redirect')
|
||||||
|
.get(SlackCallbackHandler)
|
||||||
|
|
||||||
|
export default router
|
11
server/services/groupConfig.service.ts
Normal file
11
server/services/groupConfig.service.ts
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
/*
|
||||||
|
Regenerate the group .toml configuration file
|
||||||
|
*/
|
||||||
|
|
||||||
|
import {Group} from "../entities/group.entity";
|
||||||
|
import {AppDataSource} from "../db/data-source";
|
||||||
|
|
||||||
|
|
||||||
|
export const updateGroup = async(name:string){
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue