CoreID/app/models/auth/User.model.js

338 lines
10 KiB
JavaScript
Raw Normal View History

2020-04-17 00:59:48 +00:00
const AuthUser = require('flitter-auth/model/User')
const LDAP = require('ldapjs')
const ActiveScope = require('../scopes/ActiveScope')
2020-04-22 21:56:39 +00:00
const MFAToken = require('./MFAToken.model')
2020-05-04 01:16:54 +00:00
const PasswordReset = require('./PasswordReset.model')
const AppAuthorization = require('./AppAuthorization.model')
2020-05-04 01:16:54 +00:00
const AppPassword = require('./AppPassword.model')
2020-08-13 06:56:33 +00:00
const uuid = require('uuid').v4
const NotifyConfig = require('../system/NotifyConfig.model')
2020-04-17 00:59:48 +00:00
/*
* Auth user model. This inherits fields and methods from the default
* flitter-auth/model/User model, however you can override methods and
* properties here as you need.
*/
class User extends AuthUser {
2020-04-18 00:25:33 +00:00
static get services() {
2020-05-04 01:16:54 +00:00
return [...super.services, 'auth', 'ldap_server', 'configs', 'models', 'app']
2020-04-18 00:25:33 +00:00
}
2020-04-17 00:59:48 +00:00
static get schema() {
return {...super.schema, ...{
// other schema fields here
first_name: String,
last_name: String,
2020-05-04 01:16:54 +00:00
tagline: String,
email: String,
email_verified: {type: Boolean, default: false},
ldap_visible: {type: Boolean, default: true},
active: {type: Boolean, default: true},
2020-04-22 21:56:39 +00:00
mfa_token: MFAToken,
2020-05-04 01:16:54 +00:00
password_resets: [PasswordReset],
app_passwords: [AppPassword],
app_authorizations: [AppAuthorization],
2020-04-22 21:56:39 +00:00
mfa_enabled: {type: Boolean, default: false},
2020-05-04 01:16:54 +00:00
mfa_enable_date: Date,
create_date: {type: Date, default: () => new Date},
photo_file_id: String,
2020-05-20 14:56:03 +00:00
trap: String,
notify_config: NotifyConfig,
uid_number: Number,
login_shell: String,
is_default_user_for_coreid: { type: Boolean, default: false },
2020-04-17 00:59:48 +00:00
}}
}
async grant_defaults() {
const default_user = await this.constructor.findOne({is_default_user_for_coreid: true, active: true})
this.login_shell = default_user.login_shell
this.roles = default_user.roles
this.permissions = default_user.permissions
const groups = await default_user.groups()
for ( const group of groups ) {
group.user_ids.push(this.id)
await group.save()
}
}
async get_uid_number() {
if ( !this.uid_number ) {
const Setting = this.models.get('Setting')
let last_uid = await Setting.get('ldap.last_alloc_uid')
if ( last_uid < 1 ) {
last_uid = this.configs.get('ldap:server.schema.start_uid')
}
this.uid_number = last_uid + 1
await Setting.set('ldap.last_alloc_uid', this.uid_number)
await this.save()
}
return this.uid_number
}
async photo() {
const File = this.models.get('upload::File')
return File.findById(this.photo_file_id)
}
has_authorized(client) {
return this.app_authorizations.some(x => x.client_id === client.id)
}
get_authorization(client) {
for ( const auth of this.app_authorizations ) {
if ( auth.client_id === client.id )
return auth
}
}
authorize(client) {
if ( !this.has_authorized(client) ) {
const client_rec = new AppAuthorization({
client_id: client.id,
api_scopes: client.api_scopes,
}, this)
this.app_authorizations.push(client_rec)
} else {
const client_rec = this.get_authorization(client)
client_rec.api_scopes = client.api_scopes
}
}
2020-05-12 01:26:09 +00:00
async to_api() {
return {
id: this.id,
uid: this.uid,
first_name: this.first_name,
last_name: this.last_name,
name: `${this.first_name} ${this.last_name}`,
2020-05-12 01:26:09 +00:00
email: this.email,
tagline: this.tagline,
trap: this.trap,
group_ids: (await this.groups()).map(x => x.id),
2022-01-28 04:45:11 +00:00
profile_photo: `${this.configs.get('app.url')}api/v1/auth/users/${this.uid}/photo`,
2020-05-12 01:26:09 +00:00
}
}
static scopes = [
new ActiveScope({})
]
static async ldap_directory() {
return this.find({ldap_visible: true})
}
// TODO just in case we need this later
get can_login() {
return true
}
2020-05-04 01:16:54 +00:00
async sessions() {
const Session = require('../Session')
this.app.di().inject(Session)
return Session.find({ 'session.auth.user_id': this.id })
}
async kickout() {
// TODO handle SAML session participants
const sessions = await this.sessions()
for ( const session of sessions ) {
delete session.session.auth
delete session.session.mfa_remember
await session.save()
}
}
// Prefer soft delete because of the active scope
async delete() {
this.active = false
await this.save()
}
2020-04-18 00:25:33 +00:00
async check_password(password) {
return this.get_provider().check_user_auth(this, password)
}
2020-05-04 01:16:54 +00:00
async check_app_password(password) {
for ( const pw of this.app_passwords ) {
if ( await pw.verify(password) ) {
pw.accessed = new Date
await pw.save()
return true
}
2020-05-04 01:16:54 +00:00
}
return false
}
async reset_password(new_password, reason = 'user') {
const reset = new PasswordReset({
reason,
old_hash: this.password,
}, this)
await reset.set_hash(new_password)
this.password = reset.hash
this.password_resets.push(reset)
return reset
}
async app_password(name) {
const gen = uuid().replace(/-/g, '')
const pw = new AppPassword({ name }, this)
await pw.set_hash(gen)
this.app_passwords.push(pw)
return { password: gen, record: pw }
}
async groups() {
const Group = this.models.get('auth:Group')
return Group.find({ active: true, user_ids: this.id })
}
async oidc_sessions() {
const Session = this.models.get('openid:Session')
return Session.find({ 'payload.account': this.id })
}
async logout(request) {
for ( const session of (await this.oidc_sessions()) ) {
await session.delete()
}
this.get_provider().logout(request)
}
2021-03-11 05:43:16 +00:00
async has_sudo() {
const groups = await this.groups()
return groups.some(group => group.grants_sudo)
}
async to_sudo(iam_targets = []) {
const Policy = this.models.get('iam:Policy')
const granted = []
for ( const target of iam_targets ) {
if ( await Policy.check_user_access(this, target, 'sudo') ) {
granted.push(target)
}
}
2021-03-11 05:43:16 +00:00
return {
objectClass: ['sudoRole'],
cn: `sudo_${this.uid.toLowerCase()}`,
sudoUser: this.uid.toLowerCase(),
...(granted.length ? {
iamtarget: granted,
sudoHost: 'ALL',
sudoRunAs: 'ALL',
sudoCommand: 'ALL',
} : {})
2021-03-11 05:43:16 +00:00
}
}
async to_ldap(iam_targets = []) {
const Policy = this.models.get('iam:Policy')
2021-03-10 22:02:18 +00:00
const uid_number = await this.get_uid_number()
const shell = this.login_shell || this.configs.get('ldap:server.schema.default_shell')
2021-03-11 00:37:32 +00:00
const domain = this.configs.get('ldap:server.schema.base_dc').split(',').map(x => x.replace('dc=', '')).join('.')
2021-03-10 22:02:18 +00:00
const group_ids = []
for ( const group of await this.groups() ) {
group_ids.push(await group.get_gid_number())
}
const ldap_data = {
2020-10-19 04:27:23 +00:00
uid: this.uid.toLowerCase(),
uuid: this.uuid,
cn: this.first_name,
sn: this.last_name,
gecos: `${this.first_name} ${this.last_name}`,
mail: this.email,
objectClass: ['inetOrgPerson', 'person', 'posixaccount'],
objectclass: ['inetOrgPerson', 'person', 'posixaccount'],
2020-08-20 14:11:31 +00:00
entryuuid: this.uuid,
entryUUID: this.uuid,
objectGuid: this.uuid,
objectguid: this.uuid,
2021-03-10 22:02:18 +00:00
uidNumber: uid_number,
2021-03-11 05:21:33 +00:00
gidNumber: String(await this.get_uid_number()), // group_ids.map(x => String(x)),
2021-03-11 00:31:43 +00:00
loginShell: shell,
2021-03-11 00:37:32 +00:00
homeDirectory: `/home/${this.uid}@${domain}`
}
2020-05-04 01:16:54 +00:00
if ( this.tagline ) ldap_data.extras_tagline = this.tagline
const addl_data = JSON.parse(this.data)
for ( const key in addl_data ) {
if ( !addl_data.hasOwnProperty(key) || !key.startsWith('ldap_') ) continue
ldap_data[`data${key.substr(4)}`] = `${addl_data[key]}`
}
2020-05-21 01:35:17 +00:00
const groups = await this.groups()
if ( groups.length > 0 ) {
const group_data = groups.map(x => x.dn.format(this.configs.get('ldap:server.format')))
ldap_data.memberOf = group_data
ldap_data.memberof = group_data
2020-05-04 01:16:54 +00:00
}
const iamtarget = []
for ( const target_id of iam_targets ) {
if ( await Policy.check_user_access(this, target_id) ) {
iamtarget.push(target_id)
}
}
ldap_data.iamtarget = iamtarget
return ldap_data
}
get dn() {
2020-10-19 04:27:23 +00:00
return LDAP.parseDN(`uid=${this.uid.toLowerCase()},${this.ldap_server.auth_dn().format(this.configs.get('ldap:server.format'))}`)
2020-04-18 00:25:33 +00:00
}
2020-08-13 06:56:33 +00:00
2021-03-11 05:43:16 +00:00
get sudo_dn() {
return LDAP.parseDN(`cn=sudo_${this.uid.toLowerCase()},${this.ldap_server.sudo_dn().format(this.configs.get('ldap:server.format'))}`)
}
2020-08-13 06:56:33 +00:00
// The following are used by OpenID connect
async claims(use, scope) {
return {
sub: this.id,
email: this.email,
email_verified: true, // TODO
family_name: this.last_name,
given_name: this.first_name,
locale: 'en_US', // TODO
name: `${this.first_name} ${this.last_name}`,
2020-10-19 04:27:23 +00:00
preferred_username: this.uid.toLowerCase(),
username: this.uid.toLowerCase(),
2020-08-13 06:56:33 +00:00
}
}
static async findByLogin(login) {
return this.findOne({
active: true,
2020-10-19 04:27:23 +00:00
uid: login.toLowerCase(),
2020-08-13 06:56:33 +00:00
})
}
static async findAccount(ctx, id, token) {
return this.findById(id)
}
get accountId() {
return this.id
}
2020-04-17 00:59:48 +00:00
}
module.exports = exports = User