Skip to content

Commit

Permalink
SS4 compat: Rename code to src, tabs to spaces, implement namespaces,…
Browse files Browse the repository at this point in the history
… PSR-4 autoloader
  • Loading branch information
robbieaverill committed Jan 19, 2017
1 parent d308fd2 commit 93eea8d
Show file tree
Hide file tree
Showing 13 changed files with 338 additions and 244 deletions.
17 changes: 17 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# For more information about the properties used in this file,
# please see the EditorConfig documentation:
# http://editorconfig.org

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[{*.yml,package.json}]
indent_size = 2

# The indent size used in the package.json file cannot be changed:
# https://github.com/npm/npm/pull/3180#issuecomment-16336516
5 changes: 5 additions & 0 deletions .upgrade.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mappings:
ApiKeyMemberExtension: Sminnee\ApiKey\ApiKeyMemberExtension
ApiKeyRequestFilter: Sminnee\ApiKey\ApiKeyRequestFilter
GridFieldAddApiKeyButton: Sminnee\ApiKey\GridFieldAddApiKeyButton
MemberApiKey: Sminnee\ApiKey\MemberApiKey
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
SilverStripe API Key
====================
# SilverStripe API Key

This module provides a way of creating an managing API keys within SilverStripe. This can be useful for building RESTful
and other APIs.

How it works
------------
## Requirements

* SilverStripe ^4.0
* PHP 5.5+

## How it works

* Extensions the the `SecurityAdmin` provide interfaces for seeing API keys, and generating new ones. API keys are
allocated member-by-member.
* A `RequestFilter` will look for an API key header (default: `X-API-Key`) and if it is present, authenticate the
user so that Member::currentUser() will return the corresponding member.
user so that Member::currentUser() will return the corresponding member. This should be configured by non-GraphQL
requests.
* A `ApiKeyAuthenticator` should be configured for [GraphQL](https://github.com/silverstripe/silverstripe-graphql)
request and will return the authenticated member for GraphQL contexts to use, while not applying it to the CMS
session.

Limitations
-----------
## Limitations

* You can't limit the rights that the API key has to be more granular than "all rights of the given user".
* Keys can't be disabled, only deleted
* No support for storing encrypted ("read-once") keys

Status
------
## Status

This should be considered experimental for now, and used with care. It has not received a security audit.
25 changes: 16 additions & 9 deletions _config/apikey.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
---
Name: apikey
---
Member:
SilverStripe\Security\Member:
extensions:
- ApiKeyMemberExtension
- Sminnee\ApiKey\ApiKeyMemberExtension

Injector:
RequestProcessor:
properties:
filters:
- '%$ApiKeyRequestFilter'
Sminnee\ApiKeyRequestFilter:
header_name: 'X-Api-Key'

ApiKeyRequestFilter:
header_name: 'X-Api-Key'
# For regular requests, enable the RequestFilter:
# SilverStripe\Core\Injector\Injector:
# SilverStripe\Control\RequestProcessor:
# properties:
# filters:
# - '%$ApiKeyRequestFilter'

# For GraphQL requests, enable the ApiKeyAuthenticator:
SilverStripe\GraphQL\Auth\Handler:
authenticators:
- class: Sminnee\ApiKey\ApiKeyAuthenticator
priority: 30
29 changes: 0 additions & 29 deletions code/ApiKeyMemberExtension.php

This file was deleted.

46 changes: 0 additions & 46 deletions code/ApiKeyRequestFilter.php

This file was deleted.

52 changes: 0 additions & 52 deletions code/GridFieldAddApiKeyButton.php

This file was deleted.

97 changes: 0 additions & 97 deletions code/MemberApiKey.php

This file was deleted.

16 changes: 14 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,25 @@
"description": "API Key management for SilverStripe",
"type": "silverstripe-module",
"require": {
"silverstripe/framework": "^3.1"
"silverstripe/framework": "^4.0@dev"
},
"license": "BSD-3-Clause",
"authors": [
{
"name": "Sam Minnee",
"email": "[email protected]"
}
]
],
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Sminnee\\ApiKey\\": "src/"
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
42 changes: 42 additions & 0 deletions src/ApiKeyMemberExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Sminnee\ApiKey;

use MemberApiKey;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
use SilverStripe\Forms\GridField\GridFieldDetailForm;
use SilverStripe\Forms\GridField\GridFieldEditButton;
use SilverStripe\ORM\DataExtension;

class ApiKeyMemberExtension extends DataExtension
{
private static $has_many = [
'ApiKeys' => MemberApiKey::class,
];

public function updateCMSFields(FieldList $fields)
{
$grid = $fields->dataFieldByName('ApiKeys');
if (!$grid) {
return;
}

$gridConfig = $grid->getConfig();

// Simplify view
$gridConfig->removeComponentsByType(GridFieldAddExistingAutocompleter::class);
$gridConfig->removeComponentsByType(GridFieldDetailForm::class);
$gridConfig->removeComponentsByType(GridFieldEditButton::class);

// Better add key button
$gridConfig->removeComponentsByType(GridFieldAddNewButton::class);
$gridConfig->addComponent(new GridFieldAddApiKeyButton('buttons-before-left'));

// Replace unlink with a real delete
$gridConfig->removeComponentsByType(GridFieldDeleteAction::class);
$gridConfig->addComponent(new GridFieldDeleteAction());
}
}
Loading

0 comments on commit 93eea8d

Please sign in to comment.