Skip to content

Commit

Permalink
Merge pull request #159 from gtorodelvalle/feature/support-for-the-fi…
Browse files Browse the repository at this point in the history
…ware-lab-authentication

Support for the FIWARE Lab Identity Manager for authentication
  • Loading branch information
gtorodelvalle authored Oct 17, 2016
2 parents 115d692 + 364a0da commit ffc7a1e
Show file tree
Hide file tree
Showing 79 changed files with 274 additions and 36 deletions.
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- [FEATURE] Support for the FIWARE Lab Identity Manager for authentication
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ An example simulation configuration file is shown next to give you a glimpse of
"ngsiVersion": "1.0"
},
"authentication": {
"provider": "keystone",
"protocol": "https",
"host": "localhost",
"port": 5001,
Expand Down Expand Up @@ -241,6 +242,7 @@ The simulation configuration file accepts the following JSON properties or entri
* **port**: The port where the Context Broker host machine is listening for API requests (or more concretely of the PEP protecting the access to the Context Broker API).
* **ngsiVersion**: The NGSI version to be used in the requests sent to the Context Broker. Currently, versions `1.0` and `2.0` are supported.
* **authentication**: Includes information about the Identity Service to get tokens to be included in the Context Broker requests. Optional (authentication tokens will only be requested if the `authentication` information is included).
* **provider**: The Identity Service provider from which the authorization tokens will be requested. Accepted values are: `keystone` (to request tokens for the Telefónica IoT Platform) and `fiware-lab` (to request tokens for the [FIWARE Lab cloud infrastructure](https://account.lab.fiware.org/)).
* **protocol**: The protocol the Identity Service is expecting the requests to be sent by.
* **host**: The host machine or IP where the Identity Service is running.
* **port**: The port where the Identity Service is listening for requests.
Expand Down
1 change: 1 addition & 0 deletions examples/urbo-simulation-parking.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ngsiVersion": "1.0"
},
"authentication": {
"provider": "keystone",
"protocol": "http",
"host": "1.2.3.4",
"port": 5001,
Expand Down
3 changes: 2 additions & 1 deletion examples/urbo-simulation-streetlight.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ngsiVersion": "1.0"
},
"authentication": {
"provider": "keystone",
"protocol": "http",
"host": "1.2.3.4",
"port": 5001,
Expand Down Expand Up @@ -3497,4 +3498,4 @@
]
}
]
}
}
1 change: 1 addition & 0 deletions examples/urbo-simulations-waste.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ngsiVersion": "1.0"
},
"authentication": {
"provider": "keystone",
"protocol": "http",
"host": "1.2.3.4",
"port": 5001,
Expand Down
96 changes: 62 additions & 34 deletions lib/fiwareDeviceSimulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1033,43 +1033,63 @@ function scheduleJobs() {
* @param {Function} callback The callback
*/
function requestToken(callback) {
var tokenRequestOptions = {
method: 'POST',
url: configuration.authentication.protocol + '://' + configuration.authentication.host + ':' +
configuration.authentication.port + '/v3/auth/tokens',
rejectUnauthorized: false,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
json: true,
body: {
auth: {
identity: {
methods: [
'password'
],
password: {
user: {
var tokenRequestOptions;
if (configuration.authentication.provider === 'keystone') {
tokenRequestOptions = {
method: 'POST',
url: configuration.authentication.protocol + '://' + configuration.authentication.host + ':' +
configuration.authentication.port + '/v3/auth/tokens',
rejectUnauthorized: false,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
json: true,
body: {
auth: {
identity: {
methods: [
'password'
],
password: {
user: {
domain: {
name: configuration.domain.service
},
name: configuration.authentication.user,
password: configuration.authentication.password
}
}
},
scope: {
project: {
domain: {
name: configuration.domain.service
},
name: configuration.authentication.user,
password: configuration.authentication.password
name: configuration.domain.subservice
}
}
},
scope: {
project: {
domain: {
name: configuration.domain.service
},
name: configuration.domain.subservice
}
}
}
}
};
};
} else if (configuration.authentication.provider === 'fiware-lab') {
tokenRequestOptions = {
method: 'POST',
url: configuration.authentication.protocol + '://' + configuration.authentication.host + ':' +
configuration.authentication.port + '/token',
rejectUnauthorized: false,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
json: true,
body: {
username: configuration.authentication.user,
password: configuration.authentication.password
}
};
}

request(tokenRequestOptions, function(err, response, body) {
var error;
if (err) {
Expand Down Expand Up @@ -1099,11 +1119,19 @@ function onTokenResponse(err, response, body) {
if (err || response.statusCode.toString().charAt(0) !== '2') {
end();
} else {
var expires_at = fromDate ?
new Date(Date.now() + (new Date(body.token.expires_at).getTime() - new Date(body.token.issued_at).getTime())) :
new Date(body.token.expires_at);
var token,
expires_at;
if (configuration.authentication.provider === 'keystone') {
token = response.headers['x-subject-token'];
expires_at = fromDate ?
new Date(Date.now() + (new Date(body.token.expires_at).getTime() - new Date(body.token.issued_at).getTime())) :
new Date(body.token.expires_at);
} else if (configuration.authentication.provider === 'fiware-lab') {
token = body;
expires_at = new Date(Date.now + 3600000);
}
emitTokenResponse(expires_at);
configuration.authentication.token = response.headers['x-subject-token'];
configuration.authentication.token = token;
var scheduleDate = new Date(expires_at.getTime() - 60000);
if (configuration.authentication.retry) {
scheduler.scheduleJob(scheduleDate,
Expand Down
9 changes: 8 additions & 1 deletion lib/validators/fiwareDeviceSimulatorValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,16 @@ function validateContextBrokerConfiguration(simulationConfiguration, callback) {
*/
function validateAuthenticationConfiguration(simulationConfiguration, callback) {
if (simulationConfiguration.authentication) {
if (!simulationConfiguration.authentication.provider ||
(simulationConfiguration.authentication.provider !== 'keystone' &&
simulationConfiguration.authentication.provider !== 'fiware-lab')) {
return callback(new fdsErrors.SimulationConfigurationNotValid('Invalid or no provider in the authentication ' +
'configuration information (the \'authentication.provider\' property is mandatory) ' +
'(accepted values: "keystone" and "fiware-lab")'));
}
if (!simulationConfiguration.authentication.protocol) {
return callback(new fdsErrors.SimulationConfigurationNotValid('No protocol in the authentication configuration ' +
'information (the \'authentication.host\' property is mandatory)'));
'information (the \'authentication.protocol\' property is mandatory)'));
}
if (!simulationConfiguration.authentication.host) {
return callback(new fdsErrors.SimulationConfigurationNotValid('No host in the authentication configuration ' +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ngsiVersion": "1.0"
},
"authentication": {
"provider": "keystone",
"protocol": "https",
"host": "localhost",
"port": 5001,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ngsiVersion": "1.0"
},
"authentication": {
"provider": "keystone",
"protocol": "https",
"host": "localhost",
"port": 5001,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ngsiVersion": "1.0"
},
"authentication": {
"provider": "keystone",
"protocol": "https",
"host": "localhost",
"port": 5001,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ngsiVersion": "1.0"
},
"authentication": {
"provider": "keystone",
"protocol": "https",
"host": "localhost",
"port": 5001,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ngsiVersion": "1.0"
},
"authentication": {
"provider": "keystone",
"protocol": "https",
"host": "localhost",
"port": 5001,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ngsiVersion": "1.0"
},
"authentication": {
"provider": "keystone",
"protocol": "https",
"host": "localhost",
"port": 5001,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ngsiVersion": "1.0"
},
"authentication": {
"provider": "keystone",
"protocol": "https",
"host": "localhost",
"port": 5001,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ngsiVersion": "1.0"
},
"authentication": {
"provider": "keystone",
"protocol": "https",
"host": "localhost",
"port": 5001,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ngsiVersion": "1.0"
},
"authentication": {
"provider": "keystone",
"protocol": "https",
"host": "localhost",
"port": 5001,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ngsiVersion": "1.0"
},
"authentication": {
"provider": "keystone",
"protocol": "https",
"host": "localhost",
"port": 5001,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ngsiVersion": "1.0"
},
"authentication": {
"provider": "keystone",
"protocol": "https",
"host": "localhost",
"port": 5001,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ngsiVersion": "1.0"
},
"authentication": {
"provider": "keystone",
"protocol": "https",
"host": "localhost",
"port": 5001,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ngsiVersion": "1.0"
},
"authentication": {
"provider": "keystone",
"protocol": "https",
"host": "localhost",
"port": 5001,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ngsiVersion": "1.0"
},
"authentication": {
"provider": "keystone",
"protocol": "https",
"host": "localhost",
"port": 5001,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ngsiVersion": "1.0"
},
"authentication": {
"provider": "keystone",
"protocol": "https",
"host": "localhost",
"port": 5001,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ngsiVersion": "1.0"
},
"authentication": {
"provider": "keystone",
"protocol": "https",
"host": "localhost",
"port": 5001,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ngsiVersion": "1.0"
},
"authentication": {
"provider": "keystone",
"protocol": "https",
"host": "localhost",
"port": 5001,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ngsiVersion": "1.0"
},
"authentication": {
"provider": "keystone",
"protocol": "https",
"host": "localhost",
"port": 5001,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ngsiVersion": "1.0"
},
"authentication": {
"provider": "keystone",
"protocol": "https",
"host": "localhost",
"port": 5001,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ngsiVersion": "1.0"
},
"authentication": {
"provider": "keystone",
"protocol": "https",
"host": "localhost",
"port": 5001,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ngsiVersion": "1.0"
},
"authentication": {
"provider": "keystone",
"protocol": "https",
"host": "localhost",
"port": 5001,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ngsiVersion": "1.0"
},
"authentication": {
"provider": "keystone",
"protocol": "https",
"host": "localhost",
"port": 5001,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ngsiVersion": "1.0"
},
"authentication": {
"provider": "keystone",
"protocol": "https",
"host": "localhost",
"port": 5001,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ngsiVersion": "1.0"
},
"authentication": {
"provider": "keystone",
"protocol": "https",
"host": "localhost",
"port": 5001,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ngsiVersion": "1.0"
},
"authentication": {
"provider": "keystone",
"protocol": "https",
"host": "localhost",
"port": 5001,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ngsiVersion": "1.0"
},
"authentication": {
"provider": "keystone",
"protocol": "https",
"host": "localhost",
"port": 5001,
Expand Down
Loading

0 comments on commit ffc7a1e

Please sign in to comment.