-
Notifications
You must be signed in to change notification settings - Fork 18
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 #107 from sharetribe/authorization-code
Login using an authorization code
- Loading branch information
Showing
12 changed files
with
442 additions
and
206 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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -2,27 +2,31 @@ import _ from 'lodash'; | |
|
||
const createTokenStore = () => { | ||
const tokens = []; | ||
let anonAccessCount = 0; | ||
let passwordAccessCount = 0; | ||
let passwordRefreshCount = 0; | ||
let anonAccessTokenCount = 0; | ||
let accessTokenCount = 0; | ||
let refreshTokenCount = 0; | ||
|
||
const knownUsers = [['[email protected]', 'secret-joe']]; | ||
|
||
const knownAuthorizationCodes = [ | ||
{ code: 'flex-authorization-code', username: '[email protected]' }, | ||
]; | ||
|
||
// Private | ||
|
||
const generateAnonAccessToken = () => { | ||
anonAccessCount += 1; | ||
return `anonymous-access-${anonAccessCount}`; | ||
anonAccessTokenCount += 1; | ||
return `anonymous-access-${anonAccessTokenCount}`; | ||
}; | ||
|
||
const generatePasswordAccessToken = (username, password) => { | ||
passwordAccessCount += 1; | ||
return `${username}-${password}-access-${passwordAccessCount}`; | ||
const generateAccessToken = username => { | ||
accessTokenCount += 1; | ||
return `${username}-access-${accessTokenCount}`; | ||
}; | ||
|
||
const generatePasswordRefreshToken = (username, password) => { | ||
passwordRefreshCount += 1; | ||
return `${username}-${password}-refresh-${passwordRefreshCount}`; | ||
const generateRefreshToken = username => { | ||
refreshTokenCount += 1; | ||
return `${username}-refresh-${refreshTokenCount}`; | ||
}; | ||
|
||
// Public | ||
|
@@ -45,14 +49,15 @@ const createTokenStore = () => { | |
access_token: generateAnonAccessToken(), | ||
token_type: 'bearer', | ||
expires_in: 86400, | ||
scope: 'public-read', | ||
}, | ||
}; | ||
tokens.push(token); | ||
|
||
return token.token; | ||
}; | ||
|
||
const createPasswordToken = (username, password) => { | ||
const createTokenWithCredentials = (username, password) => { | ||
const user = _.find(knownUsers, u => _.isEqual(u, [username, password])); | ||
|
||
if (!user) { | ||
|
@@ -61,14 +66,39 @@ const createTokenStore = () => { | |
|
||
const token = { | ||
token: { | ||
access_token: generatePasswordAccessToken(username, password), | ||
refresh_token: generatePasswordRefreshToken(username, password), | ||
access_token: generateAccessToken(username), | ||
refresh_token: generateRefreshToken(username), | ||
token_type: 'bearer', | ||
expires_in: 86400, | ||
scope: 'user', | ||
}, | ||
user: { | ||
username, | ||
}, | ||
}; | ||
tokens.push(token); | ||
|
||
return token.token; | ||
}; | ||
|
||
const createTokenWithAuthorizationCode = authorizationCode => { | ||
const knownCode = _.find(knownAuthorizationCodes, ({ code }) => code === authorizationCode); | ||
|
||
if (!knownCode) { | ||
return null; | ||
} | ||
|
||
const { username } = knownCode; | ||
const token = { | ||
token: { | ||
access_token: generateAccessToken(username), | ||
refresh_token: generateRefreshToken(username), | ||
token_type: 'bearer', | ||
expires_in: 86400, | ||
scope: 'user:limited', | ||
}, | ||
user: { | ||
username, | ||
password, | ||
}, | ||
}; | ||
tokens.push(token); | ||
|
@@ -88,25 +118,40 @@ const createTokenStore = () => { | |
}); | ||
}; | ||
|
||
const revokePasswordToken = refreshToken => | ||
const revokeRefreshToken = refreshToken => | ||
_.remove(tokens, t => t.token.refresh_token === refreshToken); | ||
|
||
const freshPasswordToken = refreshToken => { | ||
const existingToken = revokePasswordToken(refreshToken); | ||
const freshToken = refreshToken => { | ||
const existingToken = revokeRefreshToken(refreshToken); | ||
|
||
if (existingToken.length) { | ||
const { username, password } = existingToken[0].user; | ||
return createPasswordToken(username, password); | ||
const { username } = existingToken[0].user; | ||
|
||
const token = { | ||
token: { | ||
access_token: generateAccessToken(username), | ||
refresh_token: generateRefreshToken(username), | ||
token_type: 'bearer', | ||
expires_in: 86400, | ||
}, | ||
user: { | ||
username, | ||
}, | ||
}; | ||
tokens.push(token); | ||
|
||
return token.token; | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
return { | ||
createAnonToken, | ||
createPasswordToken, | ||
freshPasswordToken, | ||
revokePasswordToken, | ||
createTokenWithCredentials, | ||
createTokenWithAuthorizationCode, | ||
freshToken, | ||
revokeRefreshToken, | ||
validToken, | ||
expireAccessToken, | ||
}; | ||
|
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,23 @@ | ||
/** | ||
See what credentials (`username`, `password`, and `authorizationCode`) are | ||
passed in params and set the grant_type based on those. | ||
Changes to `ctx`: | ||
- add `params.grant_type` | ||
*/ | ||
export default class AddGrantTypeToParams { | ||
enter({ params, ...ctx }) { | ||
const { username, password, code } = params; | ||
|
||
if (username && password) { | ||
return { ...ctx, params: { grant_type: 'password', ...params } }; | ||
} | ||
|
||
if (code) { | ||
return { ...ctx, params: { grant_type: 'authorization_code', ...params } }; | ||
} | ||
|
||
return { ...ctx, params: { ...params } }; | ||
} | ||
} |
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 @@ | ||
/** | ||
Set the scope for a token request based on the params. | ||
Changes to `ctx`: | ||
- add `params.scope` | ||
*/ | ||
export default class AddScopeToParams { | ||
enter({ params, ...ctx }) { | ||
const { username, password } = params; | ||
|
||
if (username && password) { | ||
return { ...ctx, params: { scope: 'user', ...params } }; | ||
} | ||
|
||
return { ...ctx, params: { ...params } }; | ||
} | ||
} |
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.