Skip to content

Commit

Permalink
🎨 #26 adding prettier + git hook
Browse files Browse the repository at this point in the history
  • Loading branch information
nadnoslen committed Oct 26, 2019
1 parent 451a9c9 commit 9fe6d44
Show file tree
Hide file tree
Showing 26 changed files with 709 additions and 123 deletions.
12 changes: 3 additions & 9 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ module.exports = {
ecmaVersion: 2018,
sourceType: 'module'
},
plugins: [
'ember'
],
plugins: ['ember'],
extends: [
'eslint:recommended',
'plugin:ember/recommended'
// 'plugin:prettier/recommended'
],
env: {
browser: true
Expand All @@ -33,12 +32,7 @@ module.exports = {
'config/**/*.js',
'tests/dummy/config/**/*.js'
],
excludedFiles: [
'addon/**',
'addon-test-support/**',
'app/**',
'tests/dummy/app/**'
],
excludedFiles: ['addon/**', 'addon-test-support/**', 'app/**', 'tests/dummy/app/**'],
parserOptions: {
sourceType: 'script'
},
Expand Down
3 changes: 3 additions & 0 deletions .prettierrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://prettier.io/docs/en/options.html
printWidth: 120
singleQuote: true
5 changes: 5 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ Try to use an emoji to help describe the commit:
* 🗑 Removal
* 🚧 Work In Progress

### Prettier

`./node_modules/.bin/prettier --write "addon/**/*.{js,ts,json,css,scss,md}"`
`./node_modules/.bin/prettier --write "app/**/*.{js,ts,json,css,scss,md}"`
`./node_modules/.bin/prettier --write "tests/**/*.{js,ts,json,css,scss,md}"`

## Releasing & Publishing To NPM

Expand Down
16 changes: 10 additions & 6 deletions addon/authenticators/aws-amplify-authenticator.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { inject as service } from '@ember/service';
import { getOwner } from '@ember/application';
import { assign } from '@ember/polyfills';
import { inject as service } from '@ember/service';
import { task, timeout } from 'ember-concurrency';
import { getOwner } from '@ember/application';
import BaseAuthenticator from 'ember-simple-auth/authenticators/base';

/**
Expand All @@ -11,7 +11,6 @@ import BaseAuthenticator from 'ember-simple-auth/authenticators/base';
* the `session`'s `signIn(...)` and `confirmSignIn(...)` functions.
*/
export default BaseAuthenticator.extend({

/**
* Implementation of the `authenticate` method is dead simple; ask AWS Amplify's `Auth` to
* reach out to AWS Cognito User Pool and grab the `currentAuthenticatedUser(...)` which returns a
Expand Down Expand Up @@ -87,14 +86,16 @@ export default BaseAuthenticator.extend({
*
* @private
*/
_refreshAccessTokenTask: task(function* (exp) {
_refreshAccessTokenTask: task(function*(exp) {
const wait = exp * 1000 - Date.now();
console.warn('Scheduled token refresh will occur at ', new Date(exp * 1000));

yield timeout(wait);

console.warn('Commencing refresh of the access token at ', new Date());
return getOwner(this).lookup('session:main').restore(); // TODO: try this.restore()? trigger restore events?
return getOwner(this)
.lookup('session:main')
.restore(); // TODO: try this.restore()? trigger restore events?
}),

/**
Expand Down Expand Up @@ -124,7 +125,10 @@ export default BaseAuthenticator.extend({
.then(cognitoUser => {
this.get('_refreshAccessTokenTask').cancelAll();
this.get('_refreshAccessTokenTask').perform(cognitoUser.signInUserSession.accessToken.payload.exp);
const data = assign({ accessPayload: cognitoUser.signInUserSession.accessToken.payload }, { accessToken: cognitoUser.signInUserSession.accessToken.jwtToken });
const data = assign(
{ accessPayload: cognitoUser.signInUserSession.accessToken.payload },
{ accessToken: cognitoUser.signInUserSession.accessToken.jwtToken }
);
assign(data, { attributes: cognitoUser.attributes });
assign(data, { idPayload: cognitoUser.signInUserSession.idToken.payload });
assign(data, { idToken: cognitoUser.signInUserSession.idToken.jwtToken });
Expand Down
4 changes: 3 additions & 1 deletion addon/mixins/aws-amplify/hub.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ export default Mixin.create({
this.afterSignUp();
break;
default:
console.warn(`AWS Amplify Hub - AUTH Event -> '${capsule.payload.event}' was ignored in authenticator/aws-amplify-auth.js`);
console.warn(
`AWS Amplify Hub - AUTH Event -> '${capsule.payload.event}' was ignored in authenticator/aws-amplify-auth.js`
);
}
}
});
22 changes: 11 additions & 11 deletions addon/mixins/session/attributes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isNone, isPresent } from '@ember/utils';
import { equal, not, readOnly } from '@ember/object/computed';
import { computed, getWithDefault } from '@ember/object';
import { equal, not, readOnly } from '@ember/object/computed';
import Mixin from '@ember/object/mixin';
import { isNone, isPresent } from '@ember/utils';

/**
* This mixin provides some tangible helpers that can tranform information from your
Expand All @@ -16,7 +16,7 @@ import Mixin from '@ember/object/mixin';
export default Mixin.create({
address: readOnly('data.authenticated.attributes.address'),

authenticatedAt: computed('data.authenticated.accessPayload.auth_time', function () {
authenticatedAt: computed('data.authenticated.accessPayload.auth_time', function() {
return new Date(parseInt(this.get('data.authenticated.accessPayload.auth_time')) * 1000);
}),

Expand All @@ -28,7 +28,7 @@ export default Mixin.create({
* into account to correctly process the dates.
* @see https://openid.net/specs/openid-connect-core-1_0.html#StandardClaims
*/
birthDate: computed('data.authenticated.attributes.birthdate', function () {
birthDate: computed('data.authenticated.attributes.birthdate', function() {
if (isPresent(this.get('data.authenticated.attributes.birthdate'))) {
const splits = this.get('data.authenticated.attributes.birthdate').split('-');
if (splits[0] === '0000') {
Expand All @@ -44,7 +44,7 @@ export default Mixin.create({

familyName: readOnly('data.authenticated.attributes.family_name'),

fullName: computed('familyName', 'givenName', 'name', function () {
fullName: computed('familyName', 'givenName', 'name', function() {
if (isNone(this.get('name'))) {
return `${this.get('givenName')} ${this.get('familyName')}`;
} else {
Expand All @@ -64,7 +64,7 @@ export default Mixin.create({
/**
* Null-safe fetch of the groups assigned to the Cognito User.
*/
groups: computed('data.authenticated.accessPayload.cognito:groups', function () {
groups: computed('data.authenticated.accessPayload.cognito:groups', function() {
if (isPresent(this.get('data.authenticated.accessPayload.cognito:groups'))) {
return this.get('data.authenticated.accessPayload.cognito:groups');
}
Expand All @@ -73,11 +73,11 @@ export default Mixin.create({

'emailVerified?': readOnly('data.authenticated.attributes.email_verified'),

expiresAt: computed('data.authenticated.accessPayload.exp', function () {
expiresAt: computed('data.authenticated.accessPayload.exp', function() {
return new Date(parseInt(this.get('data.authenticated.accessPayload.exp')) * 1000);
}),

issuedAt: computed('data.authenticated.accessPayload.iat', function () {
issuedAt: computed('data.authenticated.accessPayload.iat', function() {
return new Date(parseInt(this.get('data.authenticated.accessPayload.iat')) * 1000);
}),

Expand Down Expand Up @@ -133,15 +133,15 @@ export default Mixin.create({
* number of seconds from 1970-01-01T0:0:0Z as measured in UTC until the date/time.
* @see https://openid.net/specs/openid-connect-core-1_0.html#StandardClaims
*/
updatedAt: computed('data.authenticated.attributes.updated_at', function () {
updatedAt: computed('data.authenticated.attributes.updated_at', function() {
return new Date(parseInt(this.get('data.authenticated.attributes.updated_at')) * 1000);
}),

username: readOnly('data.authenticated.accessPayload.username'),

website: readOnly('data.authenticated.attributes.website'),

writableAttributes: computed(function () {
writableAttributes: computed(function() {
return {
address: getWithDefault(this, 'address', ''),
birthdate: getWithDefault(this, 'data.authenticated.attributes.birthdate', ''),
Expand All @@ -158,7 +158,7 @@ export default Mixin.create({
profile: getWithDefault(this, 'profile', ''),
website: getWithDefault(this, 'website', ''),
zoneinfo: getWithDefault(this, 'zoneInfo', '')
}
};
}),

/**
Expand Down
16 changes: 7 additions & 9 deletions addon/mixins/session/functions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Mixin from '@ember/object/mixin';
import { inject as service } from '@ember/service';
import { typeOf } from '@ember/utils';
import Mixin from '@ember/object/mixin';
import AuthenticationState from '../../utils/authentication-state';
import MfaActivationState from '../../utils/mfa-activation-state';

Expand Down Expand Up @@ -39,7 +39,7 @@ export default Mixin.create({
completePassword(authenticationState, newPassword, additionalAttributes = {}) {
return this.get('awsAmplify.auth')
.completeNewPassword(authenticationState.get('_cognitoUser'), newPassword, additionalAttributes)
.then((cognitoUser) => {
.then(cognitoUser => {
authenticationState.set('_cognitoUser', cognitoUser);
this.authenticate('authenticator:aws-amplify-authenticator');
return authenticationState;
Expand Down Expand Up @@ -122,9 +122,9 @@ export default Mixin.create({
_cognitoUser: cognitoUser,
issuer: this.get('configuration.totpIssuerName')
});
return this.get('awsAmplify.auth').setupTOTP(cognitoUser)
return this.get('awsAmplify.auth').setupTOTP(cognitoUser);
})
.then((secret) => {
.then(secret => {
mfaActivationState.set('secret', secret);
return mfaActivationState;
})
Expand Down Expand Up @@ -184,8 +184,7 @@ export default Mixin.create({
return this.get('awsAmplify.auth')
.verifyTotpToken(mfaActivationState.get('_cognitoUser'), mfaActivationState.get('passcode'))
.then(() => {
return this.get('awsAmplify.auth')
.setPreferredMFA(mfaActivationState.get('_cognitoUser'), 'TOTP');
return this.get('awsAmplify.auth').setPreferredMFA(mfaActivationState.get('_cognitoUser'), 'TOTP');
})
.then(() => {
mfaActivationState.destroy();
Expand All @@ -203,9 +202,8 @@ export default Mixin.create({
code: 'UnprocessableEntity',
message: error,
name: 'UnprocessableEntity'
}
};
}
throw error;
}
})
;
});
5 changes: 2 additions & 3 deletions addon/services/aws-amplify/auth.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Auth from '@aws-amplify/auth';
import { Hub } from '@aws-amplify/core';
import Service, { inject as service } from '@ember/service';
import Auth from '@aws-amplify/auth';
import AwsAmplifyHub from '../../mixins/aws-amplify/hub';

/**
Expand All @@ -10,7 +10,6 @@ import AwsAmplifyHub from '../../mixins/aws-amplify/hub';
* @see https://aws-amplify.github.io/docs/js/authentication#manual-setup
*/
export default Service.extend(AwsAmplifyHub, {

/**
* A service that reads the Ember `config/environment.js` configuration for the `ember-simple-auth-aws-amplify`
* add-on.
Expand All @@ -31,5 +30,5 @@ export default Service.extend(AwsAmplifyHub, {
.then(() => console.warn('Session initialization recovered a session from local storage.'))
.catch(response => console.warn('Session initialization FAILED to recover an active session.', response));
this.set('auth', Auth);
},
}
});
7 changes: 4 additions & 3 deletions addon/services/session.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { getOwner } from '@ember/application';
import SessionService from 'ember-simple-auth/services/session';
import SessionAttributes from '../mixins/session/attributes';
import SessionFunctions from '../mixins/session/functions';
import SessionService from 'ember-simple-auth/services/session';

export default SessionService.extend(SessionAttributes, SessionFunctions, {

/**
* Trigger a forced restore of the session.
* @return {*|Promise<any>|void}
* @deprecated DO NOT USE; replaced all usages with a simple: `this.authenticate('authenticator:aws-amplify-authenticator')`
*/
forceRestore() {
return getOwner(this).lookup('session:main').restore();
return getOwner(this)
.lookup('session:main')
.restore();
}
});
3 changes: 1 addition & 2 deletions addon/utils/authentication-state.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { equal, or, readOnly } from '@ember/object/computed';
import EmberObject from '@ember/object';
import { equal, or, readOnly } from '@ember/object/computed';

/**
* An instance of `AuthenticationState` is returned from the `session` service's `signIn(...)` function.
* It allows implementors to determine whether or not they need to prompt for a MFA passcode.
*/
export default EmberObject.extend({

challengeName: readOnly('_cognitoUser.challengeName'),

init(/*_cognitoUser*/) {
Expand Down
5 changes: 2 additions & 3 deletions addon/utils/mfa-activation-state.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { and, equal, notEmpty } from '@ember/object/computed';
import EmberObject, { computed } from '@ember/object';
import { and, equal, notEmpty } from '@ember/object/computed';

/**
* An instance of `MfaActivationState` is returned from the `session` service's `setupTOTP(...)` function.
* It exists to help store state during the multiple steps that are required in configuring and activating
* an Authenticator app.
*/
export default EmberObject.extend({

/**
* Set during initialization; e.g. `MfaActivationState.create({_cognitoUser: cognitoUser})`.
* @private
Expand All @@ -25,7 +24,7 @@ export default EmberObject.extend({
/**
* @see https://github.com/google/google-authenticator/wiki/Key-Uri-Format
*/
qrcUrl: computed('_cognitoUser.signInUserSession.idToken.payload.email', 'secret', function () {
qrcUrl: computed('_cognitoUser.signInUserSession.idToken.payload.email', 'secret', function() {
const issuer = encodeURIComponent(this.get('issuer'));
const email = encodeURIComponent(this.get('_cognitoUser.signInUserSession.idToken.payload.email'));
const secret = this.get('secret');
Expand Down
20 changes: 11 additions & 9 deletions app/services/ember-simple-auth-aws-amplify/configuration.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import config from '../../config/environment';
import EmberObject, { getWithDefault } from '@ember/object';
import Service from '@ember/service';
import config from '../../config/environment';

/**
* Consumes and sets the defaults settings for the `APP.ember-simple-auth-aws-amplify` path
Expand All @@ -18,14 +18,16 @@ export default Service.extend({

this.set(
'awsAmplifyAuthConfig',
EmberObject.create(getWithDefault(config, 'APP.ember-simple-auth-aws-amplify.awsAmplifyAuth.config', {
// Amazon Cognito Region
region: 'xx-yyyyyy-#',
// Amazon Cognito User Pool ID
userPoolId: 'xx-yyyyyy-#_zzzzzzzzz',
// Amazon Cognito Web Client ID (26-char alphanumeric string)
userPoolWebClientId: 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
}))
EmberObject.create(
getWithDefault(config, 'APP.ember-simple-auth-aws-amplify.awsAmplifyAuth.config', {
// Amazon Cognito Region
region: 'xx-yyyyyy-#',
// Amazon Cognito User Pool ID
userPoolId: 'xx-yyyyyy-#_zzzzzzzzz',
// Amazon Cognito Web Client ID (26-char alphanumeric string)
userPoolWebClientId: 'xxxxxxxxxxxxxxxxxxxxxxxxxx'
})
)
);

this.set(
Expand Down
17 changes: 14 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"lint:js": "eslint .",
"start": "ember serve",
"test": "ember test",
"test:all": "ember try:each"
"test:all": "ember try:each",
"precommit": "lint-staged"
},
"dependencies": {
"@aws-amplify/auth": "^1.2.14",
Expand Down Expand Up @@ -82,14 +83,24 @@
"ember-try": "^1.2.1",
"eslint-plugin-ember": "^7.1.0",
"eslint-plugin-node": "^10.0.0",
"husky": "^3.0.9",
"lint-staged": "^9.4.2",
"loader.js": "^4.7.0",
"prettier": "^1.18.2",
"qunit-dom": "^0.9.0"
},
"engines": {
"node": "8.* || >= 10.*"
},
"ember-addon": {
"configPath": "tests/dummy/config"
"configPath": "tests/dummy/config",
"demoURL": "https://cybertooth-io.github.io/ember-simple-auth-aws-amplify/"
},
"homepage": "https://cybertooth-io.github.io/ember-simple-auth-aws-amplify"
"homepage": "https://cybertooth-io.github.io/ember-simple-auth-aws-amplify/",
"lint-staged": {
"*.{js,json,css}": [
"prettier --write",
"git add"
]
}
}
Loading

0 comments on commit 9fe6d44

Please sign in to comment.