-
-
Notifications
You must be signed in to change notification settings - Fork 436
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
OAuth2.0 #4102
base: main
Are you sure you want to change the base?
OAuth2.0 #4102
Conversation
Any suggestions? |
app/design/frontend/base/default/template/oauth2/device/verify.phtml
Outdated
Show resolved
Hide resolved
Excellent work, Hirale! Regarding reviewing this feature, it is a lot of work and we are not all OAuth 2.0 experts. I'm sure you did a lot of testing and put a ton of thought into it as you went, can you perhaps provide more of that info here? For example:
This goes for customer vs admin as well, I skimmed the code and am not sure I understand that separation well. Maybe I'm just ignorant on this, I never used M2's OAuth implementation, is it inspired by how M2 does it? |
….phtml Co-authored-by: Colin Mollenhour <[email protected]>
I developed this module because there is no headless mode for REST authentication in OpenMage.
At the beginning, I completely imitated the authorization method in the GitHub OAuth Device Flow, but the user needs to authorize the device code from the background. Since the background of OpenMage requires login, this did not meet the need for automatically obtaining tokens in headless mode. Therefore, I changed it to use the same interface for both customer and admin to authorize, adding a user_type parameter to distinguish users and verify user information. I’m open to feedback on whether this approach is appropriate or if there are better alternatives. If you have a better idea, please let me know.
<?php
// Step 1: Redirect to the authorization URL
$client_id = '6';
$redirect_uri = 'https://yourapplication.com/redirect';
$authorization_url = "https://example.com/oauth2/authorize?client_id=$client_id&redirect_uri=$redirect_uri";
echo "Go to the following URL to authorize:\n$authorization_url\n\n";
echo "After authorizing, enter the authorization code you received:\n";
// Step 2: Prompt the user to enter the authorization code
$authorization_code = trim(fgets(STDIN));
// Step 3: Exchange authorization code for access token
$client_secret = '05840bc55ccf415ef437a88f02ecce6eb4207023';
$ch = curl_init('https://example.com/oauth2/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
'grant_type' => 'authorization_code',
'code' => $authorization_code,
'client_id' => $client_id,
'redirect_uri' => $redirect_uri,
'client_secret' => $client_secret
]));
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec($ch);
curl_close($ch);
$token_info = json_decode($response, true);
if (isset($token_info['data']['access_token'])) {
$access_token = $token_info['data']['access_token'];
$refresh_token = $token_info['data']['refresh_token'];
echo "Access Token: $access_token\n";
echo "Refresh Token: $refresh_token\n";
// Step 4: Test the token
$ch = curl_init('https://example.com/api/rest/products?limit=10&page=1');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Accept-Encoding: application/json',
"Authorization: Bearer $access_token"
]);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec($ch);
curl_close($ch);
echo "Products: $response\n";
} else {
echo "Failed to obtain access token. Response: $response\n";
}
?>
<?php
$client_id = '4';
$client_secret = '30e095dedb4ccb40d42a9e5bd4074b55209e6565';
$user_type = 'admin'; // admin or customer id
$id = '8'; // admin id or customer id
$email = '[email protected]'; // admin email or customer email
// Step 1: Request a new device code
$ch = curl_init('https://example.com/oauth2/device/request');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
'client_id' => $client_id
]));
$response = curl_exec($ch);
curl_close($ch);
$device_info = json_decode($response, true);
$device_code = $device_info['data']['device_code'];
$user_code = $device_info['data']['user_code'];
echo "Device Code: $device_code\n";
echo "User Code: $user_code\n";
// Step 2: Authorize the device
$ch = curl_init('https://example.com/oauth2/device/authorize');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
'user_code' => $user_code,
'client_secret' => $client_secret,
'client_id' => $client_id,
'user_type' => $user_type,
'id' => $id,
'email' => $email
]));
$response = curl_exec($ch);
curl_close($ch);
echo "Device Authorized: $response\n";
// Step 3: Get the access token
$ch = curl_init('https://example.com/oauth2/device/poll');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
'device_code' => $device_code,
'user_type' => $user_type,
'id' => $id
]));
$response = curl_exec($ch);
curl_close($ch);
$token_info = json_decode($response, true);
$access_token = $token_info['data']['access_token'];
$refresh_token = $token_info['data']['refresh_token'];
echo "Access Token: $access_token\n";
echo "Refresh Token: $refresh_token\n";
// Step 4: Test the token
$ch = curl_init('https://example.com/api/rest/products?limit=10&page=1');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Accept-Encoding: application/json',
"Authorization: Bearer $access_token"
]);
$response = curl_exec($ch);
curl_close($ch);
echo "Products: $response\n";
?> |
You can run |
Looks good 👍 Some anoying stuff ...
|
System > Web Services > REST - OAuth2 Clients
Authorization
Authorization code (interactive)
This method works only for customers.
Open the browser and navigate to this URL: https://example.com/oauth2/authorize?client_id=1&redirect_uri=https://example.com/redirect
if customer is not logged in, customer will be redirected to login page, then navigate to this URL: https://example.com/oauth2/authorize?client_id=1&redirect_uri=https://example.com/redirect
Then customer need to clicck
Allow
button.Browser will be redirected to redirect_uri with authorization code: https://localhost/redirect?code=5a940d320d1dd5cd7eb6faf8929dfab9f3b61b38
Then go next step to get access token
Device code (headless)
Request a new device code
Authorize the device
Get the access token
Obtain access token
Authorization code
Refresh token
Test the token