auth/model/Oauth2Client.js

/**
 * @module flitter-auth/model/Oauth2Client
 */

const Model = require('flitter-orm/src/model/Model')
const uuid = require('uuid/v4')

/**
 * Flitter model spec for an authorized OAuth2 client.
 * @extends module:flitter-orm/src/model/Model~Model
 */
class Oauth2Client extends Model {
    /**
     * Define the schema for this model.
     * @returns {object}
     */
    static get schema() {
        return {
            clientID: {type: String, default: uuid},
            clientSecret: {type: String, default: uuid},
            name: String,
            grants: [String],
            redirectUris: [String],
        }
    }

    /**
     * Determines if this client can use the specified grant type.
     * @param {string} grant - the name of the grant type - e.g. 'authorization_code'
     * @returns {boolean} - true if the client can use that grant type
     */
    can(grant) {
        return this.grants.includes(grant)
    }

    /**
     * Allows the client to use the specified grant type.
     * @param {string} grant - the name of the grant type - e.g. 'authorization_code'
     */
    allow(grant) {
        if ( !this.can(grant) ) this.grants.push(grant)
    }
}

module.exports = exports = Oauth2Client