fleet/website/api/controllers/android-proxy/create-android-enrollment-token.js
Eric 1799c824b1
Website: Update Android proxy endpoints exits (#34135)
Changes:
- Updated the website's Android proxy endpoints to use action2 exit
signals.
2025-10-10 17:11:42 -05:00

88 lines
3.2 KiB
JavaScript
Vendored

module.exports = {
friendlyName: 'Create android enrollment token',
description: 'Creates and returns an enrollment token for an Android enterprise',
inputs: {
androidEnterpriseId: {
type: 'string',
required: true,
},
},
exits: {
success: { description: 'An Android Enterprise enrollment token was returned to the Fleet instance.'},
missingAuthHeader: { description: 'This request was missing an authorization header.', responseType: 'unauthorized'},
unauthorized: { description: 'Invalid authentication token', responseType: 'unauthorized'},
notFound: { description: 'No Android enterprise found for this Fleet server.', responseType: 'notFound'},
},
fn: async function ({androidEnterpriseId}) {
// Extract fleetServerSecret from the Authorization header
let authHeader = this.req.get('authorization');
let fleetServerSecret;
if (authHeader && authHeader.startsWith('Bearer')) {
fleetServerSecret = authHeader.replace('Bearer', '').trim();
} else {
throw 'missingAuthHeader';
}
// Authenticate this request
let thisAndroidEnterprise = await AndroidEnterprise.findOne({
androidEnterpriseId: androidEnterpriseId,
});
// Return a 404 response if no records are found.
if(!thisAndroidEnterprise) {
throw 'notFound';
}
// Return an unauthorized response if the provided secret does not match.
if(thisAndroidEnterprise.fleetServerSecret !== fleetServerSecret) {
throw 'unauthorized';
}
// Check the list of Android Enterprises managed by Fleet to see if this Android Enterprise is still managed.
let isEnterpriseManagedByFleet = await sails.helpers.androidProxy.getIsEnterpriseManagedByFleet(androidEnterpriseId);
// Return a 404 response if this Android enterprise is no longer managed by Fleet.
if(!isEnterpriseManagedByFleet) {
throw 'notFound';
}
let newEnrollmentToken = await sails.helpers.flow.build(async ()=>{
let { google } = require('googleapis');
let androidmanagement = google.androidmanagement('v1');
let googleAuth = new google.auth.GoogleAuth({
scopes: ['https://www.googleapis.com/auth/androidmanagement'],
credentials: {
client_email: sails.config.custom.androidEnterpriseServiceAccountEmailAddress,// eslint-disable-line camelcase
private_key: sails.config.custom.androidEnterpriseServiceAccountPrivateKey,// eslint-disable-line camelcase
},
});
// Acquire the google auth client, and bind it to all future calls
let authClient = await googleAuth.getClient();
google.options({auth: authClient});
// [?]: https://googleapis.dev/nodejs/googleapis/latest/androidmanagement/classes/Resource$Enterprises$Enrollmenttokens.html#create
let enrollmentTokenCreateResponse = await androidmanagement.enterprises.enrollmentTokens.create({
parent: `enterprises/${androidEnterpriseId}`,
requestBody: this.req.body,
});
return enrollmentTokenCreateResponse.data;
}).intercept((err)=>{
return new Error(`When attempting to create an enrollment token for an Android enterprise (${androidEnterpriseId}), an error occurred. Error: ${err}`);
});
return newEnrollmentToken;
}
};