This repository has been archived by the owner on Mar 19, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #183 from mozilla-services/fxa-oauth
Bug 1041814 — FxA oauth support
- Loading branch information
Showing
12 changed files
with
525 additions
and
7 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
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 |
---|---|---|
|
@@ -6,5 +6,6 @@ | |
"INVALID_AUTH_TOKEN": 110, | ||
"EXPIRED": 111, | ||
"REQUEST_TOO_LARGE": 113, | ||
"INVALID_OAUTH_STATE": 114, | ||
"BACKEND": 201 | ||
} |
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,137 @@ | ||
/* 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'; | ||
|
||
var randomBytes = require('crypto').randomBytes; | ||
var request = require('request'); | ||
var sendError = require('../utils').sendError; | ||
var errors = require('../errno.json'); | ||
var hmac = require('../hmac'); | ||
|
||
module.exports = function (app, conf, logError, storage, auth) { | ||
|
||
var oauthConf = conf.get('fxaOAuth'); | ||
|
||
/** | ||
* Provide the client with the parameters needed for the OAuth dance. | ||
**/ | ||
app.get('/fxa-oauth/parameters', auth.requireHawkSession, | ||
function(req, res) { | ||
var callback = function(state) { | ||
res.status(200).json({ | ||
client_id: oauthConf.client_id, | ||
redirect_uri: oauthConf.redirect_uri, | ||
oauth_uri: oauthConf.oauth_uri, | ||
scope: oauthConf.scope, | ||
state: state | ||
}); | ||
}; | ||
storage.getHawkOAuthState(req.hawkIdHmac, function(err, state) { | ||
if (res.serverError(err)) return; | ||
if (state === null) { | ||
state = randomBytes(32).toString('hex'); | ||
storage.setHawkOAuthState(req.hawkIdHmac, state, function(err) { | ||
if (res.serverError(err)) return; | ||
callback(state); | ||
}); | ||
} else { | ||
callback(state); | ||
} | ||
}); | ||
}); | ||
|
||
/** | ||
* Returns the current status of the hawk session (e.g. if it's authenticated | ||
* or not. | ||
**/ | ||
app.get('/fxa-oauth/token', auth.requireHawkSession, function (req, res) { | ||
storage.getHawkOAuthToken(req.hawkIdHmac, function(err, token) { | ||
if (res.serverError(err)) return; | ||
res.status(200).json({ | ||
oauthToken: token || undefined | ||
}); | ||
}); | ||
}); | ||
|
||
/** | ||
* Trade an OAuth code with an oauth bearer token. | ||
**/ | ||
app.post('/fxa-oauth/token', auth.requireHawkSession, function (req, res) { | ||
var state = req.query.state; | ||
var code = req.query.code; | ||
|
||
var missingParams = []; | ||
if (!state) { | ||
missingParams.push('state'); | ||
} | ||
if (!code) { | ||
missingParams.push('code'); | ||
} | ||
if (missingParams.length > 0) { | ||
sendError(res, 400, errors.MISSING_PARAMETERS, | ||
"Missing: " + missingParams.join(", ")); | ||
return; | ||
} | ||
|
||
// State should match an existing state. | ||
storage.getHawkOAuthState(req.hawkIdHmac, function(err, storedState) { | ||
if (res.serverError(err)) return; | ||
|
||
storage.clearHawkOAuthState(req.hawkIdHmac, function(err) { | ||
if (res.serverError(err)) return; | ||
}); | ||
|
||
if (storedState !== state) { | ||
// Reset session state after an attempt was made to compare it. | ||
sendError(res, 400, | ||
errors.INVALID_OAUTH_STATE, "Invalid OAuth state"); | ||
return; | ||
} | ||
|
||
// Trade the OAuth code for a token. | ||
request.post({ | ||
uri: oauthConf.oauth_uri + '/token', | ||
json: { | ||
code: code, | ||
client_id: oauthConf.client_id, | ||
client_secret: oauthConf.client_secret | ||
} | ||
}, function (err, r, body) { | ||
if (res.serverError(err)) return; | ||
|
||
var token = body.access_token; | ||
|
||
// store the bearer token | ||
storage.setHawkOAuthToken(req.hawkIdHmac, token); | ||
|
||
// Make a request to the FxA server to have information about the | ||
// profile. | ||
request.get({ | ||
uri: oauthConf.profile_uri + '/profile', | ||
headers: { | ||
Authorization: 'Bearer ' + token | ||
} | ||
}, function (err, r, body) { | ||
if (res.serverError(err)) return; | ||
var data; | ||
try { | ||
data = JSON.parse(body); | ||
} catch (e) { | ||
if (res.serverError(new Error(e + " JSON: " + body))) return; | ||
} | ||
// Store the appropriate profile information into the database, | ||
// associated with the hawk session. | ||
var userHmac = hmac(data.email, conf.get('userMacSecret')); | ||
storage.setHawkUser(userHmac, req.hawkIdHmac, function(err) { | ||
if (res.serverError(err)) return; | ||
res.status(200).json({ | ||
oauthToken: token | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}; |
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,18 @@ | ||
/* 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'; | ||
|
||
module.exports = function (app, auth) { | ||
|
||
/** | ||
* An endpoint you can use to retrieve a hawk session. | ||
* | ||
* In the case of OAuth, this session will be upgraded at the end of the | ||
* authentication flow, with an attached identity. | ||
**/ | ||
app.post('/session', auth.attachOrCreateHawkSession, function(req, res) { | ||
res.status(204).json(); | ||
}); | ||
}; |
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
Oops, something went wrong.