-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UUID, Add JWT for authentication
- Loading branch information
1 parent
4d60736
commit 16c58ef
Showing
11 changed files
with
212 additions
and
9 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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const jwt = require('jsonwebtoken'); | ||
|
||
class JsonWebToken { | ||
static getNewJsonWebToken(options) { | ||
const tokenOptionalInfo = { | ||
algorithm: 'HS256', | ||
expiresIn: options.expiration, | ||
}; | ||
|
||
return jwt.sign( | ||
options.tokenPayload, | ||
options.secret, | ||
tokenOptionalInfo); | ||
} | ||
} | ||
|
||
module.exports = JsonWebToken; |
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,12 @@ | ||
class MongoDb { | ||
static saveEmailUser(user) { | ||
// Fake saving by just return the user object | ||
return user; | ||
} | ||
|
||
static isUserExists(email) { | ||
return false; | ||
} | ||
} | ||
|
||
module.exports = MongoDb; |
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,19 @@ | ||
/** | ||
* Unique Id class is responsible for generating a Universally unique identifier | ||
*/ | ||
const uuidV4 = require('uuid/v4'); | ||
|
||
class UniqueId { | ||
/** | ||
* getNewUuid() Generate new Universally unique identifier | ||
*/ | ||
static getNewUuid() { | ||
/** | ||
* Generate a v4 UUID (random) | ||
* https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_.28random.29 | ||
*/ | ||
return uuidV4(); | ||
} | ||
} | ||
|
||
module.exports = UniqueId; |
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,16 @@ | ||
class Authentication { | ||
constructor(options) { | ||
this.uniqueIdAdapter = options.UniqueIdAdapter; | ||
this.webTokenAdapter = options.WebTokenAdapter; | ||
} | ||
|
||
getNewUserId() { | ||
return this.uniqueIdAdapter.getNewUuid(); | ||
} | ||
|
||
getNewWebToken(options) { | ||
return this.webTokenAdapter.getNewJsonWebToken(options); | ||
} | ||
} | ||
|
||
module.exports = Authentication; |
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,15 @@ | ||
class Database { | ||
constructor(options) { | ||
this.databaseAdapter = options.DatabaseAdapter; | ||
} | ||
|
||
saveEmailUser(user) { | ||
return this.databaseAdapter.saveEmailUser(user); | ||
} | ||
|
||
isUserExists(email) { | ||
return this.databaseAdapter.isUserExists(email); | ||
} | ||
} | ||
|
||
module.exports = Database; |
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 |
---|---|---|
|
@@ -3,27 +3,55 @@ | |
*/ | ||
const EnvironmentVariables = require('./infrastructures/environment-variables'); | ||
const ExpressWebServer = require('./infrastructures/express-server'); | ||
const JsonWebToken = require('./infrastructures/json-web-token'); | ||
const MongoDb = require('./infrastructures/mongodb'); | ||
const UniqueId = require('./infrastructures/unique-id'); | ||
|
||
const ConfigurationAdapter = require('./interfaces/configuration'); | ||
const AuthenticationInterface = require('./interfaces/authentication'); | ||
const ConfigurationInterface = require('./interfaces/configuration'); | ||
const DatabaseInterface = require('./interfaces/database'); | ||
const WebServerInterface = require('./interfaces/webserver'); | ||
|
||
const ConfigurationInteractor = require('./usecases/configuration'); | ||
const VersionInteractor = require('./usecases/version'); | ||
const UserInteractor = require('./usecases/user'); | ||
|
||
const environmentVariable = new EnvironmentVariables(); | ||
const configurationAdapter = new ConfigurationAdapter({ | ||
const configurationInterface = new ConfigurationInterface({ | ||
ConfigurationAdapter: environmentVariable, | ||
}); | ||
|
||
const configurationInteractor = new ConfigurationInteractor({ | ||
ConfigurationInterface: configurationAdapter, | ||
ConfigurationInterface: configurationInterface, | ||
}); | ||
|
||
const configuraionData = configurationInteractor.load(); | ||
|
||
// Print out the current configuration data for testing purpose | ||
console.log(configuraionData.toString()); | ||
|
||
const authenticationInterface = new AuthenticationInterface({ | ||
UniqueIdAdapter: UniqueId, | ||
WebTokenAdapter: JsonWebToken, | ||
}); | ||
|
||
const databaseInterface = new DatabaseInterface({ | ||
DatabaseAdapter: MongoDb, | ||
}); | ||
|
||
const userInteractor = new UserInteractor({ | ||
ConfigurationData: configuraionData, | ||
AuthenticationInterface: authenticationInterface, | ||
DatabaseInterface: databaseInterface, | ||
}); | ||
|
||
const output = userInteractor.createNewEmailUser({ | ||
Email: '[email protected]', | ||
Password: 'secret', | ||
}); | ||
|
||
console.log(JSON.stringify(output, null, 2)); | ||
|
||
const versionInteractor = new VersionInteractor(); | ||
const webserverInterface = new WebServerInterface({ | ||
VersionInteractor: versionInteractor, | ||
|
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