Add plugin auth migrations
This commit is contained in:
parent
e307e4fce3
commit
055cfb11a9
7 changed files with 94 additions and 49 deletions
|
@ -1,30 +0,0 @@
|
|||
import * as Sequelize from 'sequelize'
|
||||
|
||||
async function up (utils: {
|
||||
transaction: Sequelize.Transaction
|
||||
queryInterface: Sequelize.QueryInterface
|
||||
sequelize: Sequelize.Sequelize
|
||||
}): Promise<void> {
|
||||
|
||||
const metadata = {
|
||||
type: Sequelize.JSONB,
|
||||
allowNull: true
|
||||
}
|
||||
await utils.queryInterface.addColumn('videoFile', 'metadata', metadata)
|
||||
|
||||
const metadataUrl = {
|
||||
type: Sequelize.STRING,
|
||||
allowNull: true
|
||||
}
|
||||
await utils.queryInterface.addColumn('videoFile', 'metadataUrl', metadataUrl)
|
||||
|
||||
}
|
||||
|
||||
function down (options) {
|
||||
throw new Error('Not implemented.')
|
||||
}
|
||||
|
||||
export {
|
||||
up,
|
||||
down
|
||||
}
|
42
server/initializers/migrations/0490-plugin-auth.ts
Normal file
42
server/initializers/migrations/0490-plugin-auth.ts
Normal file
|
@ -0,0 +1,42 @@
|
|||
import * as Sequelize from 'sequelize'
|
||||
|
||||
async function up (utils: {
|
||||
transaction: Sequelize.Transaction
|
||||
queryInterface: Sequelize.QueryInterface
|
||||
sequelize: Sequelize.Sequelize
|
||||
}): Promise<void> {
|
||||
|
||||
{
|
||||
const password = {
|
||||
type: Sequelize.STRING,
|
||||
allowNull: true
|
||||
}
|
||||
await utils.queryInterface.changeColumn('user', 'password', password)
|
||||
}
|
||||
|
||||
{
|
||||
const pluginAuth = {
|
||||
type: Sequelize.STRING,
|
||||
allowNull: true
|
||||
}
|
||||
await utils.queryInterface.addColumn('user', 'pluginAuth', pluginAuth)
|
||||
}
|
||||
|
||||
{
|
||||
const authName = {
|
||||
type: Sequelize.STRING,
|
||||
allowNull: true
|
||||
}
|
||||
await utils.queryInterface.addColumn('oAuthToken', 'authName', authName)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function down (options) {
|
||||
throw new Error('Not implemented.')
|
||||
}
|
||||
|
||||
export {
|
||||
up,
|
||||
down
|
||||
}
|
|
@ -126,26 +126,30 @@ async function proxifyPasswordGrant (req: express.Request, res: express.Response
|
|||
authOptions.authName, pluginAuth.npmName, loginOptions.id, authOptions.getWeight()
|
||||
)
|
||||
|
||||
const loginResult = await authOptions.login(loginOptions)
|
||||
if (loginResult) {
|
||||
logger.info(
|
||||
'Login success with auth method %s of plugin %s for %s.',
|
||||
authOptions.authName, pluginAuth.npmName, loginOptions.id
|
||||
)
|
||||
try {
|
||||
const loginResult = await authOptions.login(loginOptions)
|
||||
if (loginResult) {
|
||||
logger.info(
|
||||
'Login success with auth method %s of plugin %s for %s.',
|
||||
authOptions.authName, pluginAuth.npmName, loginOptions.id
|
||||
)
|
||||
|
||||
res.locals.bypassLogin = {
|
||||
bypass: true,
|
||||
pluginName: pluginAuth.npmName,
|
||||
authName: authOptions.authName,
|
||||
user: {
|
||||
username: loginResult.username,
|
||||
email: loginResult.email,
|
||||
role: loginResult.role || UserRole.USER,
|
||||
displayName: loginResult.displayName || loginResult.username
|
||||
res.locals.bypassLogin = {
|
||||
bypass: true,
|
||||
pluginName: pluginAuth.npmName,
|
||||
authName: authOptions.authName,
|
||||
user: {
|
||||
username: loginResult.username,
|
||||
email: loginResult.email,
|
||||
role: loginResult.role || UserRole.USER,
|
||||
displayName: loginResult.displayName || loginResult.username
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
return
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error('Error in auth method %s of plugin %s', authOptions.authName, pluginAuth.npmName, { err })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -198,6 +198,8 @@ export class RegisterHelpersStore {
|
|||
return {
|
||||
getSetting: (name: string) => PluginModel.getSetting(this.plugin.name, this.plugin.type, name),
|
||||
|
||||
getSettings: (names: string[]) => PluginModel.getSettings(this.plugin.name, this.plugin.type, names),
|
||||
|
||||
setSetting: (name: string, value: string) => PluginModel.setSetting(this.plugin.name, this.plugin.type, name, value)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -129,6 +129,31 @@ export class PluginModel extends Model<PluginModel> {
|
|||
})
|
||||
}
|
||||
|
||||
static getSettings (pluginName: string, pluginType: PluginType, settingNames: string[]) {
|
||||
const query = {
|
||||
attributes: [ 'settings' ],
|
||||
where: {
|
||||
name: pluginName,
|
||||
type: pluginType
|
||||
}
|
||||
}
|
||||
|
||||
return PluginModel.findOne(query)
|
||||
.then(p => {
|
||||
if (!p || !p.settings) return {}
|
||||
|
||||
const result: { [settingName: string ]: string } = {}
|
||||
|
||||
for (const key of Object.keys(p.settings)) {
|
||||
if (settingNames.includes(key)) {
|
||||
result[key] = p.settings[key]
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
})
|
||||
}
|
||||
|
||||
static setSetting (pluginName: string, pluginType: PluginType, settingName: string, settingValue: string) {
|
||||
const query = {
|
||||
where: {
|
||||
|
|
|
@ -143,7 +143,7 @@ describe('Test id and pass auth plugins', function () {
|
|||
expect(body.role).to.equal(UserRole.MODERATOR)
|
||||
})
|
||||
|
||||
it('Should correctly auth token of laguna', async function () {
|
||||
it('Should reject token of laguna by the plugin hook', async function () {
|
||||
this.timeout(10000)
|
||||
|
||||
await wait(5000)
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
import * as Bluebird from 'bluebird'
|
||||
|
||||
export interface PluginSettingsManager {
|
||||
getSetting: (name: string) => Bluebird<string>
|
||||
getSetting: (name: string) => Bluebird<string | boolean>
|
||||
|
||||
getSettings: (names: string[]) => Bluebird<{ [settingName: string]: string | boolean }>
|
||||
|
||||
setSetting: (name: string, value: string) => Bluebird<any>
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue