-
Notifications
You must be signed in to change notification settings - Fork 1.7k
modoboa api v2 compatibility #9987
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
Open
jquiros2
wants to merge
4
commits into
roundcube:master
Choose a base branch
from
jquiros2:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+47
−28
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,11 +2,14 @@ | |
|
|
||
| /** | ||
| * Modoboa Password Driver | ||
| * Minor modification from 2.0, updated to use Modoboa v2 password endpoint. | ||
| * Payload uses form-encoded data: current password and new password. | ||
| * Endpoint: PUT /api/v2/accounts/{id}/password/ | ||
| * | ||
| * Payload is json string containing username, oldPassword and newPassword | ||
| * Return value is a json string saying result: true if success. | ||
| * Return value is a status constant (PASSWORD_SUCCESS on success, | ||
| * PASSWORD_CONNECT_ERROR on failure). | ||
| * | ||
| * @version 2.0 | ||
| * @version 2.1 | ||
| * | ||
| * @author stephane @actionweb.fr | ||
| * | ||
|
|
@@ -25,9 +28,9 @@ | |
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see https://www.gnu.org/licenses/. | ||
| * | ||
| * The driver need modoboa core 1.10.6 or later | ||
| * The driver needs Modoboa core 2.0.0 or later | ||
| * | ||
| * You need to define theses variables in plugin/password/config.inc.php | ||
| * You need to define these variables in plugin/password/config.inc.php | ||
| * | ||
| * $config['password_driver'] = 'modoboa'; // use modoboa as driver | ||
| * $config['password_modoboa_api_token'] = ''; // put token number from Modoboa server | ||
|
|
@@ -40,59 +43,75 @@ public function save($curpass, $passwd, $username) | |
| { | ||
| // Init config access | ||
| $rcmail = rcmail::get_instance(); | ||
| $token = $rcmail->config->get('password_modoboa_api_token'); | ||
| $token = $rcmail->config->get('password_modoboa_api_token'); | ||
| $IMAPhost = $_SESSION['imap_host']; | ||
| // uncomment and add your url if you use a differente url for your modoboa instance and api: | ||
| //$IMAPhost = 'mymodoboa.somewhere.net'; | ||
|
|
||
| // Use v2 API: search user by username | ||
| $client = password::get_http_client(); | ||
| $url = "https://{$IMAPhost}/api/v1/accounts/?search=" . urlencode($username); | ||
| $url = "https://{$IMAPhost}/api/v2/accounts/?search=" . urlencode($username); | ||
|
|
||
| $options = [ | ||
| 'http_errors' => true, | ||
| 'headers' => [ | ||
| 'Authorization' => "Token {$token}", | ||
| 'Cache-Control' => 'no-cache', | ||
| 'Content-Type' => 'application/json', | ||
| 'Authorization' => "Token " . $token, | ||
| 'Accept' => 'application/json', | ||
| ], | ||
| ]; | ||
|
|
||
| // Call GET to fetch values from modoboa server | ||
| // Call GET to fetch user details | ||
| try { | ||
| $response = $client->get($url, $options); | ||
| $response = $response->getBody()->getContents(); | ||
| $responseBody = $response->getBody()->getContents(); | ||
| } catch (\Exception $e) { | ||
| rcube::raise_error("Password plugin: Error fetching {$url} : {$e->getMessage()}", true); | ||
| return PASSWORD_CONNECT_ERROR; | ||
| } | ||
|
|
||
| // Decode json string | ||
| $decoded = json_decode($response); | ||
| $decoded = json_decode($responseBody); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needless variable rename. |
||
|
|
||
| if (!is_array($decoded)) { | ||
| if (!is_array($decoded) || empty($decoded)) { | ||
| return PASSWORD_CONNECT_ERROR; | ||
| } | ||
|
|
||
| // Get user ID (pk) | ||
| $userid = $decoded[0]->pk; | ||
|
|
||
| // Encode json with new password | ||
| $options['body'] = json_encode([ | ||
| 'username' => $decoded[0]->username, | ||
| 'mailbox' => $decoded[0]->mailbox, | ||
| 'role' => $decoded[0]->role, | ||
| 'password' => $passwd, // new password | ||
| ]); | ||
|
|
||
| $url = "https://{$IMAPhost}/api/v1/accounts/{$userid}/"; | ||
| // Call v2 password endpoint with form-encoded data | ||
| $url2 = "https://{$IMAPhost}/api/v2/accounts/" . $userid . "/password/"; | ||
| $options2 = [ | ||
| 'http_errors' => true, | ||
| 'headers' => [ | ||
| 'Authorization' => "Token " . $token, | ||
| 'Accept' => 'application/json', | ||
| // Content-Type will be set automatically when using form_params | ||
| ], | ||
| 'form_params' => [ | ||
| 'password' => $curpass, // current password | ||
| 'new_password' => $passwd // new password | ||
| ], | ||
| ]; | ||
|
|
||
| // Call HTTP API Modoboa | ||
| // Execute password change | ||
| try { | ||
| $response = $client->put($url, $options); | ||
| $response = $response->getBody()->getContents(); | ||
| $response2 = $client->put($url2, $options2); | ||
| $responseBody2 = $response2->getBody()->getContents(); | ||
| $httpCode = $response2->getStatusCode(); | ||
| } catch (\Exception $e) { | ||
| rcube::raise_error("Password plugin: Error on {$url} : {$e->getMessage()}", true); | ||
| rcube::raise_error("Password plugin: Error on {$url2} : {$e->getMessage()}", true); | ||
| return PASSWORD_CONNECT_ERROR; | ||
| } | ||
|
|
||
| return PASSWORD_SUCCESS; | ||
| // Check for success | ||
| if ($httpCode >= 200 && $httpCode < 300) { | ||
| return PASSWORD_SUCCESS; | ||
| } | ||
|
|
||
| // Log for debugging if needed | ||
| rcube::raise_error("Password plugin: Unexpected response from {$url2}. HTTP {$httpCode}. Response: {$responseBody2}", true); | ||
| return PASSWORD_CONNECT_ERROR; | ||
| } | ||
| } | ||
| ?> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No closing tags, please. |
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, remove this comment or introduce a new config option.