Skip to content

Commit

Permalink
Refactor to manage the oauth2_server entity with Features.
Browse files Browse the repository at this point in the history
  • Loading branch information
paul121 committed Jan 28, 2020
1 parent 6fef5f2 commit 281b582
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 41 deletions.
4 changes: 3 additions & 1 deletion modules/farm/farm_api/farm_api_oauth/farm_api_oauth.info
Expand Up @@ -6,5 +6,7 @@ dependencies[] = farm_api
dependencies[] = oauth2_server
dependencies[] = restws
dependencies[] = restws_oauth2_server

dependencies[] = entity
features[features_api][] = api:2
features[oauth2_server][] = farmos_oauth

44 changes: 4 additions & 40 deletions modules/farm/farm_api/farm_api_oauth/farm_api_oauth.module
Expand Up @@ -5,6 +5,8 @@
* Farm API OAuth Module.
*/

include_once 'farm_api_oauth.features.inc';

/**
* Implements hook_permission().
*/
Expand Down Expand Up @@ -303,43 +305,6 @@ function farm_api_oauth_enable(){
// Set variable so other modules can use the oauth2_server.
variable_set('farmos_oauth2_server_name', $server_name);

// Create an OAuth2 Server.
$server = entity_create('oauth2_server', array());
$server->name = $server_name;
$server->label = 'Farm OAuth Server';
$server->settings = array(
'enforce_state' => TRUE,
'allow_implicit' => FALSE,
'use_openid_connect' => FALSE,
'use_crypto_tokens' => FALSE,
'store_encrypted_token_string' => FALSE,
'require_exact_redirect_uri' => TRUE,
'grant_types' => array(
'authorization_code' => 'authorization_code',
'refresh_token' => 'refresh_token',
'password' => 'password',
),
'always_issue_new_refresh_token' => TRUE,
'access_lifetime' => 3600,
'id_lifetime' => 3600,
'refresh_token_lifetime' => 1209600,
);
$server->save();

// Create an OAuth2 Scope for restws access.
$scope = entity_create('oauth2_server_scope', array());
$scope->server = $server->name;
$scope->name = 'user_access';
$scope->description = 'Allow full user access to farmOS records.';
$scope->save();

// Create an OAuth2 Scope for farm info.
$scope = entity_create('oauth2_server_scope', array());
$scope->server = $server->name;
$scope->name = 'farm_info';
$scope->description = 'Allow access to farmOS server info.';
$scope->save();

// Add community-trusted OAuth Clients.
$enabled_clients = array(
'farmos_api_client'=>'farmos_api_client',
Expand Down Expand Up @@ -380,6 +345,8 @@ function farm_api_oauth_disable(){
}
}

// Features will delete the OAuth2 Server entity.
// Features does not delete the existing OAuth Scopes.
// Delete farmOS OAuth Scopes.
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'oauth2_server_scope')
Expand All @@ -390,9 +357,6 @@ function farm_api_oauth_disable(){
entity_delete('oauth2_server_scope', $scope_ids);
}

// Delete farmOS OAuth2 Server.
entity_delete('oauth2_server', $server_name);

// Delete variables
variable_del('restws_oauth2_server_name');
variable_del('farmos_oauth2_server_name');
Expand Down

0 comments on commit 281b582

Please sign in to comment.