Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] API Key auth #9869

Closed
wants to merge 38 commits into from
Closed

[WIP] API Key auth #9869

wants to merge 38 commits into from

Conversation

kevinansfield
Copy link
Member

@kevinansfield kevinansfield commented Sep 13, 2018

refs #9865

  • adds integrations and api_keys tables
  • adds Admin API Client role
  • adds Integration model
  • adds ApiKey model
    • creates default secret if not given
    • hardcodes associated role based on key type
      • admin = Admin API Client
      • content = no role
  • adds API Key authentication
  • sets req.api_key and context.api_key for API key requests
  • updates can-this and the Model.permissible functions to work with context.api_key
    • permissions providers.apiKey added to fetch role permissions associated with the api key
  • sets up integrations and api_keys test fixtures

TODO:

  • determine why post create/edit tests are failing with 422 'Value in [users.name] cannot be blank.'. Likely either or both:
    • API needs a user passed in because it can't retrieve it from context.user
    • created/updated_by is dependent on context.user
  • invalid accesstoken test is throwing 500 rather than 401
  • fix broken permissions tests
  • add user model permissions tests for api keys being unable to assign Owner permissions
  • review TODOs
    • how to handle invites created via api key
    • test for e-mail test not being trigger able via api key
    • review context.user usage in api/users.handlePermissions
    • log api key when making requests
    • always allow staticPages v2 Admin API requests (separate PR)
  • review naming
    • API vs Api
    • api_key vs apiKey

Separate PRs:

  • sync up middleware with session auth, rename authenticatePrivate
  • how to handle "internal" clients (scheduler + backup)
  • set created/updated/published_by columns to api key value
  • update api_key.lastSeenAt value for authenticated api key requests
  • integration and api_key routes and controllers

@kevinansfield kevinansfield changed the title API v2 - Client auth [WIP] API v2 - Client auth Sep 13, 2018
@kevinansfield kevinansfield force-pushed the api-key-auth branch 2 times, most recently from ac1057f to d94d210 Compare September 13, 2018 13:27
context: {internal: true}
}, options);

// TODO: does this rule need adjusting in our eslint config?

This comment was marked as abuse.

nullable: false,
validations: {isIn: [['content', 'admin']]}
},
secret: {type: 'string', maxlength: 64, nullable: false, unique: true},

This comment was marked as abuse.

}));
}

// TODO: should we do Buffer.from(x, 'hex') or is using the secret as-is ok?

This comment was marked as abuse.

This comment was marked as abuse.

try {
// TODO: grab the decoded payload and check if the payload endpoint
// matches the requested endpoint
jwt.verify(token, apiKey.get('secret'), JWT_OPTIONS);

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

let [scheme, credentials] = header.split(' ');

if (/^Bearer$/i.test(scheme)) {
let [apiKeyId, token] = credentials.split('|');

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

return next();
}

let {apiKeyId, token} = extractCredentialsFromHeader(req.headers.authorization);

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

* @param {string} header
*/
const extractCredentialsFromHeader = function extractCredentialsFromHeader(header) {
let [scheme, credentials] = header.split(' ');

This comment was marked as abuse.

@kevinansfield kevinansfield force-pushed the api-key-auth branch 2 times, most recently from 8a725e2 to 6b95f5f Compare September 18, 2018 14:07
@@ -91,11 +101,11 @@ CanThisResult.prototype.buildObjectTypeHandlers = function (objTypes, actType, c
// Offer a chance for the TargetModel to override the results
if (TargetModel && _.isFunction(TargetModel.permissible)) {
return TargetModel.permissible(
modelId, actType, context, unsafeAttrs, loadedPermissions, hasUserPermission, hasAppPermission
modelId, actType, context, unsafeAttrs, loadedPermissions, hasUserPermission, hasApiKeyPermission, hasAppPermission

This comment was marked as abuse.

@kevinansfield
Copy link
Member Author

This has been superseded by separate PRs. Closing.

@kirrg001 kirrg001 deleted the api-key-auth branch February 5, 2019 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants