Skip to content

Commit b991356

Browse files
committed
Initial version
1 parent 5a44f77 commit b991356

10 files changed

+261
-1
lines changed

Diff for: Plugin.php

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php namespace LukeTowers\AzureADSSO;
2+
3+
use App;
4+
use View;
5+
use Event;
6+
use Config;
7+
use System\Classes\PluginBase;
8+
use System\Classes\CombineAssets;
9+
use Illuminate\Foundation\AliasLoader;
10+
use Backend\Controllers\Auth as AuthController;
11+
12+
/**
13+
* AzureADSSO Plugin Information File
14+
*/
15+
class Plugin extends PluginBase
16+
{
17+
public $elevated = true;
18+
19+
/**
20+
* Returns information about this plugin.
21+
*
22+
* @return array
23+
*/
24+
public function pluginDetails()
25+
{
26+
return [
27+
'name' => 'AzureAD SSO',
28+
'description' => 'Adds support for logging into the backend with Azure AD SSO OAuth',
29+
'author' => 'LukeTowers',
30+
'icon' => 'icon-lock'
31+
];
32+
}
33+
34+
/**
35+
* Boot method, called right before the request route.
36+
*
37+
* @return array
38+
*/
39+
public function boot()
40+
{
41+
AuthController::extend(function($controller) {
42+
$controller->bindEvent('page.beforeDisplay', function ($action, $params) {
43+
if ($action === 'params') {
44+
$controller->addCss(CombineAssets::combine(['azureadsso.css'], plugins_path('luketowers/azureadsso/assets/css/')));
45+
}
46+
});
47+
});
48+
49+
Event::listen('backend.auth.extendSigninView', function($controller) {
50+
return View::make("luketowers.azureadsso::login");
51+
});
52+
53+
$this->bootPackages();
54+
$this->extendAzureAD();
55+
}
56+
57+
/**
58+
* Boots (configures and registers) any packages found within this plugin's packages.load configuration value
59+
*
60+
* @see https://luketowers.ca/blog/how-to-use-laravel-packages-in-october-plugins
61+
* @author Luke Towers <[email protected]>
62+
*/
63+
public function bootPackages()
64+
{
65+
// Get the namespace of the current plugin to use in accessing the Config of the plugin
66+
$pluginNamespace = str_replace('\\', '.', strtolower(__NAMESPACE__));
67+
68+
// Instantiate the AliasLoader for any aliases that will be loaded
69+
$aliasLoader = AliasLoader::getInstance();
70+
71+
// Get the packages to boot
72+
$packages = Config::get($pluginNamespace . '::packages');
73+
74+
// Boot each package
75+
foreach ($packages as $name => $options) {
76+
// Setup the configuration for the package, pulling from this plugin's config
77+
if (!empty($options['config']) && !empty($options['config_namespace'])) {
78+
Config::set($options['config_namespace'], $options['config']);
79+
}
80+
81+
// Register any Service Providers for the package
82+
if (!empty($options['providers'])) {
83+
foreach ($options['providers'] as $provider) {
84+
App::register($provider);
85+
}
86+
}
87+
88+
// Register any Aliases for the package
89+
if (!empty($options['aliases'])) {
90+
foreach ($options['aliases'] as $alias => $path) {
91+
$aliasLoader->alias($alias, $path);
92+
}
93+
}
94+
}
95+
}
96+
97+
/**
98+
* Extend the base library used to make it compatible with OctoberCMS
99+
*
100+
* @return void
101+
*/
102+
protected function extendAzureAD()
103+
{
104+
// Process the user object before saving it
105+
\Metrogistics\AzureSocialite\UserFactory::userCallback(function($newUser) {
106+
// Generate a random password for the user
107+
$pass = str_random(60);
108+
$newUser->password = $pass;
109+
$newUser->password_confirmation = $pass;
110+
111+
// Ensure that the user has an email address
112+
if (empty($newUser->email) && !empty($newUser->alt_email)) {
113+
$newUser->email = $newUser->alt_email;
114+
}
115+
116+
// @TODO: Enable assigning a default role to new users
117+
118+
// Clean up
119+
unset($newUser->attributes['alt_email']);
120+
});
121+
}
122+
}

Diff for: README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# About
2+
3+
Adds support for logging into the backend with Azure Active Directory Single Sign On (SSO) OAuth.
4+
5+
# Installation
6+
7+
To install from the [Marketplace](https://octobercms.com/plugin/luketowers-azureadsso), click on the "Add to Project" button and then select the project you wish to add it to before updating the project to pull in the plugin.
8+
9+
To install from the backend, go to **Settings -> Updates & Plugins -> Install Plugins** and then search for `LukeTowers.AzureADSSO`.
10+
11+
To install from [the repository](https://github.com/luketowers/oc-azureadsso-plugin), clone it into **plugins/luketowers/azureadsso** and then run `composer update` from your project root in order to pull in the dependencies.
12+
13+
To install it with Composer, run `composer require luketowers/oc-azureadsso-plugin` from your project root.
14+
15+
# Setup
16+
17+
1. Go to [`Azure Active Directory` -> `App registrations`](https://aad.portal.azure.com/#blade/Microsoft_AAD_IAM/ActiveDirectoryMenuBlade/RegisteredAppsPreview)
18+
2. Create a new application (registration)
19+
3. Choose a name (Example: "My OctoberCMS Application Sign-in Helper")
20+
4. If asked, select the "Web app / API" Application Type
21+
5. Provide the Redirect URI (by default will be `https://example.com/luketowers/azureadsso/login/microsoft/callback`, replace `https://example.com` with the URL to your OctoberCMS instance)
22+
6. Click Register
23+
7. Select your newly created application
24+
8. Copy the "Application (client) ID" value and put it into your `.env` file for the `AZURE_AD_CLIENT_ID` env variable
25+
9. Select the permissions required for your app in the "API Permissions" tab (recommended at least Microsft Graph -> `User.Read`, `email`, & `profile`)
26+
10. Go to the Certificates & Secrets tab and create a new Client Secret (recommended to set it to "Never" expire). Copy this value down and use it for the `AZURE_AD_CLIENT_SECRET` env variable in your `.env` file.

Diff for: assets/css/azureadsso.css

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: assets/images/login-dark.png

775 Bytes
Loading

Diff for: assets/images/login-light.png

804 Bytes
Loading

Diff for: composer.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "luketowers/oc-azureadsso-plugin",
3+
"type": "october-plugin",
4+
"description": "OctoberCMS plugin to add support for logging into the backend with Azure AD SSO OAuth.",
5+
"keywords": ["october", "octobercms", "laravel", "azure", "sso", "single sign on", "active directory"],
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Luke Towers",
10+
"email": "[email protected]"
11+
}
12+
],
13+
"require": {
14+
"composer/installers": "~1.0",
15+
"metrogistics/laravel-azure-ad-oauth": "^1.2"
16+
},
17+
"autoload": {
18+
"files": [
19+
"helpers.php"
20+
]
21+
}
22+
}

Diff for: config/config.php

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php return [
2+
'packages' => [
3+
'metrogistics/laravel-azure-ad-oauth' => [
4+
'providers' => [
5+
'\Metrogistics\AzureSocialite\ServiceProvider'
6+
],
7+
8+
'aliases' => [
9+
'AzureUser' => '\Metrogistics\AzureSocialite\AzureUserFacade'
10+
],
11+
12+
'config_namespace' => 'azure-oath',
13+
14+
'config' => [
15+
'routes' => [
16+
// The middleware to wrap the auth routes in.
17+
// Must contain session handling otherwise login will fail.
18+
'middleware' => 'web',
19+
20+
// The url that will redirect to the SSO URL.
21+
'login' => 'luketowers/azureadsso/login/microsoft',
22+
23+
// The app route that SSO will redirect to
24+
// Make sure you update credentials.redirect as well
25+
'callback' => 'luketowers/azureadsso/login/microsoft/callback',
26+
],
27+
'credentials' => [
28+
'client_id' => env('AZURE_AD_CLIENT_ID', ''),
29+
'client_secret' => env('AZURE_AD_CLIENT_SECRET', ''),
30+
'redirect' => Request::root().'/luketowers/azureadsso/login/microsoft/callback'
31+
],
32+
33+
// The route to redirect the user to upon login.
34+
'redirect_on_login' => Backend::url(),
35+
36+
// The User Eloquent class.
37+
'user_class' => '\Backend\Models\User',
38+
39+
// How much time should be left before the access
40+
// token expires to attempt a refresh.
41+
'refresh_token_within' => 30,
42+
43+
// The users table database column to store the user SSO ID.
44+
'user_id_field' => 'azure_id',
45+
46+
// How to map azure user fields to Laravel user fields.
47+
// Do not include the id field above.
48+
// AzureUserField => LaravelUserField
49+
'user_map' => [
50+
'givenName' => 'first_name',
51+
'surname' => 'last_name',
52+
'email' => 'email',
53+
'userPrincipalName' => 'alt_email',
54+
]
55+
]
56+
]
57+
]
58+
];

Diff for: updates/add_azure_id_backend_users.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php namespace LukeTowers\AzureADSSO\Updates;
2+
3+
use Schema;
4+
use October\Rain\Database\Updates\Migration;
5+
6+
class AddAzureIdBackendUsers extends Migration
7+
{
8+
public function up()
9+
{
10+
if (!Schema::hasColumn('backend_users', 'azure_id')) {
11+
Schema::table('backend_users', function ($table) {
12+
$table->string('azure_id');
13+
});
14+
}
15+
}
16+
17+
public function down()
18+
{
19+
if (Schema::hasColumn('backend_users', 'azure_id')) {
20+
Schema::table('backend_users', function ($table) {
21+
$table->dropColumn('azure_id');
22+
});
23+
}
24+
}
25+
}

Diff for: updates/version.yaml

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
1.0.1: First version of AzureADSSO
1+
1.0.1:
2+
- First version of AzureADSSO
3+
- add_azure_id_backend_users.php

Diff for: views/login.blade.php

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div id="azureadsso-container">
2+
<a id="azureadsso-button" href="{{ \Url::to(\Config::get('azure-oath.login', 'luketowers/azureadsso/login/microsoft')) }}"></a>
3+
</div>

0 commit comments

Comments
 (0)