-
Notifications
You must be signed in to change notification settings - Fork 56
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
Support deployments using Matrix Authentication Service #577
Open
H-Shay
wants to merge
6
commits into
main
Choose a base branch
from
shay/MAS_integration
base: main
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.
+418
−6
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
aee4d17
add client for mas compatibility
H-Shay 9486966
update deactivate command to use mas
H-Shay 51f4ce1
add mas-compatible lock/unlock command
H-Shay 2128488
lint
H-Shay 421a9a8
add deps
H-Shay d1e4fba
another lint
H-Shay 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 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/* | ||
Copyright 2025 The Matrix.org Foundation C.I.C. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { ClientCredentials, AccessToken } from "simple-oauth2"; | ||
import { IConfig } from "./config"; | ||
import axios from "axios"; | ||
import { LogService } from "@vector-im/matrix-bot-sdk"; | ||
|
||
export class MASclient { | ||
public readonly config: IConfig; | ||
private client: ClientCredentials; | ||
private accessToken: AccessToken; | ||
|
||
constructor(config: IConfig) { | ||
LogService.info("MAS client", "Setting up mas client"); | ||
this.config = config; | ||
const clientConfig = { | ||
client: { | ||
id: config.mas.clientId, | ||
secret: config.mas.clientSecret, | ||
}, | ||
auth: { | ||
tokenPath: config.mas.url + "/oauth2/token", | ||
tokenHost: config.mas.url, | ||
}, | ||
}; | ||
this.client = new ClientCredentials(clientConfig); | ||
} | ||
|
||
public async getAccessToken() { | ||
if (!this.accessToken || this.accessToken.expired()) { | ||
// fetch a new one | ||
const tokenParams = { scope: "urn:mas:admin" }; | ||
try { | ||
this.accessToken = await this.client.getToken(tokenParams); | ||
} catch (error) { | ||
LogService.error("MAS client", "Error fetching auth token for MAS:", error.message); | ||
throw error; | ||
} | ||
} | ||
return this.accessToken; | ||
} | ||
|
||
public async getMASUserId(userId: string): Promise<string> { | ||
const index = userId.indexOf(":"); | ||
const localpart = userId.substring(1, index); | ||
const accessToken = await this.getAccessToken(); | ||
try { | ||
const resp = await axios({ | ||
method: "get", | ||
url: this.config.mas.url + `/api/admin/v1/users/by-username/${localpart}`, | ||
headers: { | ||
"User-Agent": "Mjolnir", | ||
"Content-Type": "application/json; charset=UTF-8", | ||
"Authorization": `Bearer ${accessToken.token.access_token}`, | ||
}, | ||
}); | ||
return resp.data.data.id; | ||
} catch (error) { | ||
LogService.error("MAS client", `Error fetching MAS id for user ${userId}:`, error.message); | ||
throw error; | ||
} | ||
} | ||
|
||
public async deactivateMasUser(userId: string): Promise<void> { | ||
const masId = await this.getMASUserId(userId); | ||
const accessToken = await this.getAccessToken(); | ||
const url = this.config.mas.url + `/api/admin/v1/users/${masId}/deactivate`; | ||
try { | ||
await axios({ | ||
method: "post", | ||
url: url, | ||
headers: { | ||
"User-Agent": "Mjolnir", | ||
"Content-Type": "application/json; charset=UTF-8", | ||
"Authorization": `Bearer ${accessToken.token.access_token}`, | ||
}, | ||
}); | ||
Comment on lines
+83
to
+91
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. moving the axios calls to a |
||
} catch (error) { | ||
LogService.error("MAS client", `Error deactivating user ${userId} via MAS`, error.message); | ||
throw error; | ||
} | ||
} | ||
|
||
public async lockMasUser(userId: string): Promise<void> { | ||
const masId = await this.getMASUserId(userId); | ||
const accessToken = await this.getAccessToken(); | ||
try { | ||
await axios({ | ||
method: "post", | ||
url: this.config.mas.url + `/api/admin/v1/users/${masId}/lock`, | ||
headers: { | ||
"User-Agent": "Mjolnir", | ||
"Content-Type": "application/json; charset=UTF-8", | ||
"Authorization": `Bearer ${accessToken.token.access_token}`, | ||
}, | ||
}); | ||
} catch (error) { | ||
LogService.error("Mas client", `Error locking user ${userId} via MAS:`, error.message); | ||
throw error; | ||
} | ||
} | ||
|
||
public async unlockMasUser(userId: string): Promise<void> { | ||
const masId = await this.getMASUserId(userId); | ||
const accessToken = await this.getAccessToken(); | ||
try { | ||
await axios({ | ||
method: "post", | ||
url: this.config.mas.url + `/api/admin/v1/users/${masId}/unlock`, | ||
headers: { | ||
"User-Agent": "Mjolnir", | ||
"Content-Type": "application/json; charset=UTF-8", | ||
"Authorization": `Bearer ${accessToken.token.access_token}`, | ||
}, | ||
}); | ||
} catch (error) { | ||
LogService.error("Mas client", `Error unlocking user ${userId} via MAS:`, error.message); | ||
throw error; | ||
} | ||
} | ||
|
||
public async masUserIsAdmin(userId: string): Promise<boolean> { | ||
const index = userId.indexOf(":"); | ||
const localpart = userId.substring(1, index); | ||
const accessToken = await this.getAccessToken(); | ||
|
||
let resp; | ||
try { | ||
resp = await axios({ | ||
method: "get", | ||
url: this.config.mas.url + `/api/admin/v1/users/by-username/${localpart}`, | ||
headers: { | ||
"User-Agent": "Janitor", | ||
"Content-Type": "application/json; charset=UTF-8", | ||
"Authorization": `Bearer ${accessToken.token.access_token}`, | ||
}, | ||
}); | ||
} catch (error) { | ||
LogService.error("MAS client", `Error determining if MAS user ${userId} is admin: `, error.message); | ||
throw error; | ||
} | ||
return resp.data.data.attributes.admin; | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
Copyright 2025 The Matrix.org Foundation C.I.C. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { Mjolnir } from "../Mjolnir"; | ||
import { LogLevel, RichReply } from "@vector-im/matrix-bot-sdk"; | ||
|
||
// !mjolnir lock <user ID> | ||
export async function execLockCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) { | ||
const target = parts[2]; | ||
|
||
const isAdmin = await mjolnir.isSynapseAdmin(); | ||
if (!isAdmin) { | ||
const message = "I am not a Synapse administrator, or the endpoint is blocked"; | ||
const reply = RichReply.createFor(roomId, event, message, message); | ||
reply["msgtype"] = "m.notice"; | ||
mjolnir.client.sendMessage(roomId, reply); | ||
return; | ||
} | ||
|
||
if (mjolnir.usingMas) { | ||
try { | ||
await mjolnir.masClient.lockMasUser(target); | ||
} catch (err) { | ||
mjolnir.managementRoomOutput.logMessage( | ||
LogLevel.ERROR, | ||
"Lock Command", | ||
`There was an error locking ${target}, please check the logs for more information.`, | ||
); | ||
await mjolnir.client.unstableApis.addReactionToEvent(roomId, event["event_id"], "❌"); | ||
return; | ||
} | ||
} else { | ||
await mjolnir.lockSynapseUser(target); | ||
} | ||
await mjolnir.client.unstableApis.addReactionToEvent(roomId, event["event_id"], "✅"); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
slight inconsistency in naming: is it
MAS
orMas
in functions? (either is fine - we should match existing styles if there's examples).