9
9
*/
10
10
11
11
// No direct call
12
- if ( !defined ( 'YOURLS_ABSPATH ' ) ) die ();
12
+ if (!defined ('YOURLS_ABSPATH ' )) {
13
+ die ();
14
+ }
13
15
14
16
/* Assumes that you have already downloaded and installed the
15
17
* Google APIs Client Library for PHP and it's in the same directory.
16
18
* See https://github.com/google/google-api-php-client for install instructions.
17
19
* Include your composer dependencies:
18
20
*/
19
- require_once __DIR__ . '/vendor/autoload.php ' ;
21
+ require_once __DIR__ . '/vendor/autoload.php ' ;
20
22
21
23
/* The function yourls_is_valid_user() in includes/functions-auth.php checks for a valid user via the login
22
24
* form or stored cookie. The 'shunt_is_valid_user' filter allows plugins such as this one, to short-circuit
26
28
/* This says: when filter 'shunt_is_valid_user' is triggered, execute function 'atomarch_google_auth'
27
29
* and send back it's return value. Filters should always have a return value.
28
30
*/
29
- yourls_add_filter ( 'shunt_is_valid_user ' , 'atomarch_google_auth ' );
31
+ yourls_add_filter ('shunt_is_valid_user ' , 'atomarch_google_auth ' );
30
32
31
33
function atomarch_google_auth () {
32
34
@@ -39,64 +41,62 @@ function atomarch_google_auth() {
39
41
// See https://developers.google.com/api-client-library/php/auth/web-app to create
40
42
// an OAuth 2.0 client ID, and download the resulting JSON file
41
43
// This assumes that client_secrets.json file resides in the same directory as plugin.php
42
- $ client ->setAuthConfig ( dirname (__FILE__ ) .'/client_secrets.json ' );
44
+ $ client ->setAuthConfig (dirname (__FILE__ ) . '/client_secrets.json ' );
43
45
44
- $ client ->setRedirectUri ( yourls_admin_url () );
46
+ $ client ->setRedirectUri (yourls_admin_url ());
45
47
46
48
if (isset ($ _SESSION ['access_token ' ]) && $ _SESSION ['access_token ' ]) {
47
49
// User has already authenticated against google with an approved domain, nothing to do
48
50
return true ;
49
51
50
52
} else {
51
53
52
- if (! isset ($ _GET ['code ' ])) {
54
+ if (!isset ($ _GET ['code ' ])) {
53
55
54
- // Generate a URL to request access from Google's OAuth 2.0 server
55
- $ auth_url = $ client ->createAuthUrl ();
56
- // Redirect the user to $auth_url so they can enter their Google credentials
57
- header ('Location: ' . filter_var ($ auth_url , FILTER_SANITIZE_URL ));
56
+ // Generate a URL to request access from Google's OAuth 2.0 server
57
+ $ auth_url = $ client ->createAuthUrl ();
58
+ // Redirect the user to $auth_url so they can enter their Google credentials
59
+ header ('Location: ' . filter_var ($ auth_url , FILTER_SANITIZE_URL ));
58
60
59
- } else {
60
- // Exchange an authorization code for an access token
61
+ } else {
62
+ // Exchange an authorization code for an access token
61
63
$ token = $ client ->fetchAccessTokenWithAuthCode ($ _GET ['code ' ]);
62
64
//Store Access Token in a session variable
63
65
$ _SESSION ['access_token ' ] = $ token ;
64
66
65
- if ( atomarch_check_domain ($ client ) === false ) {
67
+ if (atomarch_check_domain ($ client ) === false ) {
66
68
$ client ->revokeToken ();
67
69
unset($ _SESSION ['access_token ' ]);
68
- yourls_e ( "User from Unauthorized Domain. " );
70
+ yourls_e ("User from Unauthorized Domain. " );
69
71
die ();
70
72
}
71
73
72
- $ redirect_uri = yourls_admin_url ();
73
- header ('Location: ' . filter_var ($ redirect_uri , FILTER_SANITIZE_URL ));
74
- }
74
+ $ redirect_uri = yourls_admin_url ();
75
+ header ('Location: ' . filter_var ($ redirect_uri , FILTER_SANITIZE_URL ));
76
+ }
75
77
}
76
78
}
77
79
78
- function atomarch_check_domain ( $ google_client ) {
80
+ function atomarch_check_domain ($ google_client ) {
79
81
80
82
// List of domains that have permission to login. Use "*"" to allow access from any google account
81
83
//$APPROVED_DOMAINS = array("domain1.com", "domain2.com");
82
84
$ APPROVED_DOMAINS = array ("* " );
83
85
84
- if ( in_array ("* " , $ APPROVED_DOMAINS ) ) {
85
- return true ;
86
+ if (in_array ("* " , $ APPROVED_DOMAINS )) {
87
+ return true ;
86
88
}
87
89
88
90
if (isset ($ _SESSION ['access_token ' ]) && $ _SESSION ['access_token ' ]) {
89
91
90
92
$ google_oauthV2 = new Google_Service_Oauth2 ($ google_client );
91
93
$ user_info = $ google_oauthV2 ->userinfo ->get ();
92
- $ user_domain = substr (strrchr ($ user_info ['email ' ], "@ " ), 1 );
94
+ $ user_domain = substr (strrchr ($ user_info ['email ' ], "@ " ), 1 );
93
95
94
- if ( in_array ($ user_domain , $ APPROVED_DOMAINS ) ) {
95
- return true ;
96
- } else {
97
- return false ;
98
- }
96
+ if (in_array ($ user_domain , $ APPROVED_DOMAINS )) {
97
+ return true ;
98
+ } else {
99
+ return false ;
100
+ }
99
101
}
100
102
}
101
-
102
- ?>
0 commit comments