Skip to content

Commit

Permalink
Merge pull request #190 from BuckinghamAJ/dev
Browse files Browse the repository at this point in the history
Grabbing Gov email in Login.gov all_emails
  • Loading branch information
BuckinghamAJ authored Nov 19, 2024
2 parents 0e4983f + 2b7be3a commit e612858
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
30 changes: 26 additions & 4 deletions server/routes/auth.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ try {
* @return {*|PromiseLike<T | never>|Promise<T | never>}
*/
function updateMAXUser(cas_data, user) {
let now = new Date()
let date = (now.getMonth() + 1) + "-" + now.getDate() + "-" + now.getFullYear()

//update existing
try {
user['firstName'] = cas_data['first-name']
Expand All @@ -69,7 +72,7 @@ function updateMAXUser(cas_data, user) {
user['isRejected'] = false
user['isAccepted'] = true
user['tempPassword'] = null
user['creationDate'] = Date.now()
user['creationDate'] = date
return user.save()
.then(() => {
return user['id']
Expand Down Expand Up @@ -111,18 +114,26 @@ function capitalize(s)
return s[0].toUpperCase() + s.slice(1);
}

function getGovernmentEmail(emails) {
return emails.find(email => email.endsWith('.gov') || email.endsWith('.mil')) || null;
}

function createUser(loginGovUser) {
let now = new Date()
let date = (now.getMonth() + 1) + "-" + now.getDate() + "-" + now.getFullYear()

//console.log("Login.gov user:", loginGovUser)

const gov_email = getGovernmentEmail(loginGovUser.all_emails || [])

const user_email = gov_email || loginGovUser.email

let user_data = {
'firstName': loginGovUser.given_name || null,
'lastName': loginGovUser.family_name || null,
'email': loginGovUser.email,
'email': user_email,
'password': null,
'agency': grabAgencyFromEmail(loginGovUser.email),
'agency': grabAgencyFromEmail(user_email),
'position': '',
'userRole': 'Executive User', // If we need to handle user roles, we should set it to lowest setting and adjust
'isRejected': false,
Expand Down Expand Up @@ -276,7 +287,15 @@ async function createOrUpdateMAXUser(cas_data) {

async function createOrUpdateLoginGovUser(login_gov_data) {
try {
let u = await User.findOne({where: {'email': login_gov_data["email"]}})
let u = await User.findOne({
where: {
[Op.or]: [
{ email: login_gov_data.email },
{ email: { [Op.in]: login_gov_data.all_emails || [] } }
]
}
});

if (u) {
return updateUser(login_gov_data, u)
} else {
Expand Down Expand Up @@ -554,6 +573,8 @@ module.exports = {
srt_userinfo.user.sessionEnd = Math.floor ((new Date().getTime() + ms(getConfig('sessionLength')) )/ 1000)

logger.log('info', (srt_userinfo.email || userInfo.email) + ' authenticated with LOGIN.GOV', {cas_userinfo: srt_userinfo, tag: 'Login.gov Auth Token'})

console.log("srt_userinfo: ", srt_userinfo)

let uri_components = {
token: jwt.sign({access_token: accessToken, user: srt_userinfo.user, sessionEnd: srt_userinfo.sessionEnd, token_life_in_seconds: getConfig('renewTokenLife')}, common.jwtSecret, { expiresIn: getConfig('renewTokenLife') }),
Expand Down Expand Up @@ -685,6 +706,7 @@ module.exports = {
isGSAAdmin : isGSAAdmin,
passwordOnlyWhitelist: userOnPasswordOnlyWhitelist,
translateCASAgencyName: translateCASAgencyName,
getGovernmentEmail: getGovernmentEmail,

roles : roles,
roleKeys : roleKeys,
Expand Down
2 changes: 1 addition & 1 deletion server/routes/prediction.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ module.exports = {
let keys = Object.keys(req.body)

// verify that only supported filter params are used
let validKeys = ['agency', 'office', 'numDocs', 'solNum', 'category_list', 'startDate', 'fromPeriod', 'endDate', 'toPeriod']
let validKeys = ['agency', 'office', 'numDocs', 'solNum', 'category_list', 'startDate', 'fromPeriod', 'endDate', 'toPeriod', 'noticeType']
// add in the keys used by the PrimeNG table lazy loader
validKeys.push('first', 'filters', 'globalFilter', 'multiSortMeta', 'rows', 'sortField', 'sortOrder')
for (let i = 0; i < keys.length; i++) {
Expand Down
25 changes: 24 additions & 1 deletion server/tests/auth.routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const User = require('../models').User
const authRoutes = require('../routes/auth.routes')
const logger = require('../config/winston')
const mocks = require('./mocks')
const { getGovernmentEmail } = require('../routes/auth.routes');

const { userAcceptedCASData } = require('./test.data');

Expand Down Expand Up @@ -526,4 +527,26 @@ describe('/api/auth/', () => {
{'cas_userinfo':{'email-address': '[email protected]'}})).toBeFalse();
})

})
})

describe('getGovernmentEmail', () => {
test('returns the first .gov email if present', () => {
const emails = ['[email protected]', '[email protected]', '[email protected]'];
expect(getGovernmentEmail(emails)).toBe('[email protected]');
});

test('returns the first .mil email if present', () => {
const emails = ['[email protected]', '[email protected]', '[email protected]'];
expect(getGovernmentEmail(emails)).toBe('[email protected]');
});

test('returns null if no .gov or .mil email is present', () => {
const emails = ['[email protected]', '[email protected]'];
expect(getGovernmentEmail(emails)).toBe(null);
});

test('returns null if the emails array is empty', () => {
const emails = [];
expect(getGovernmentEmail(emails)).toBe(null);
});
});

0 comments on commit e612858

Please sign in to comment.