This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(oauth): notify push and email on code exchanges
- Loading branch information
Showing
8 changed files
with
126 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
'use strict' | ||
|
||
const Joi = require('joi') | ||
const validators = require('../routes/validators') | ||
|
||
module.exports = (config) => { | ||
return { | ||
path: '/v1/verify', | ||
method: 'POST', | ||
validate: { | ||
payload: { | ||
token: validators.accessToken.required(), | ||
}, | ||
response: { | ||
user: Joi.string().required(), | ||
client_id: Joi.string().required(), | ||
scope: Joi.array(), | ||
profile_changed_at: Joi.number().min(0) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
'use strict' | ||
|
||
const ScopeSet = require('fxa-shared').oauth.scopes | ||
|
||
// right now we only care about notifications for the following scopes | ||
// if not a match, then we don't notify | ||
const NOTIFICATION_SCOPES = ScopeSet.fromArray(['https://identity.mozilla.com/apps/oldsync']) | ||
|
||
module.exports = { | ||
newTokenNotification: async function newTokenNotification (db, oauthdb, mailer, devices, request, credentials, grant) { | ||
const scopeSet = ScopeSet.fromString(grant.scope) | ||
|
||
if (! scopeSet.intersects(NOTIFICATION_SCOPES)) { | ||
// right now we only care about notifications for the `oldsync` scope | ||
// if not a match, then we don't do any notifications | ||
return | ||
} | ||
|
||
const tokenVerify = await oauthdb.checkAccessToken({ | ||
token: grant.access_token | ||
}) | ||
|
||
if (! credentials) { | ||
credentials = {} | ||
} | ||
|
||
const uid = tokenVerify.user | ||
credentials.uid = uid | ||
credentials.tokenVerified = true // XXX TODO: check this? | ||
credentials.refreshTokenId = grant.refresh_token | ||
credentials.client = await oauthdb.getClientInfo(tokenVerify.client_id) | ||
|
||
await devices.upsert(request, credentials, {}) | ||
|
||
const geoData = request.app.geo | ||
const ip = request.app.clientAddress | ||
const service = request.payload.service || request.query.service || tokenVerify.client_id | ||
|
||
const emailOptions = { | ||
acceptLanguage: request.app.acceptLanguage, | ||
ip: ip, | ||
location: geoData.location, | ||
service: service, | ||
timeZone: geoData.timeZone, | ||
uid: credentials.uid | ||
} | ||
|
||
const account = await db.account(uid) | ||
await mailer.sendNewDeviceLoginNotification(account.emails, account, emailOptions) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters