officectl


重要

我在 happn 工作期间参与了这个项目。
在那里的时候,我可以访问许多目录,并能够在这些目录上测试 officectl。

我已经不在 happn 工作了!
虽然我完全致力于修复问题并帮助想要使用 officectl 的人,但我无法对目前支持的大多数目录保持积极主动。


REST API

基础

所有请求(除了身份验证请求)都必须包含以下标头

Authorization: Bearer <token>

所有响应都按如下格式格式化

{
   "error": ErrorObject or null
   "data": CallTypeDependentObject or null
}
If error is null, data won’t be null and vice-versa.

对象类型

TaggedID 对象

   This is a string with the following format:
     service_id + ":" + id_of_object_for_given_service

   Obviously the service_id cannot contain a colon. The id of the object might though.

Error 对象

{
   "code": Int
   "domain": String
   "message": String
}

User 对象

{
   "id": TaggedID
   "linked_ids": [
      TaggedID,
      TaggedID,
      ...
   ]

   "first_name": String or null
   "last_name": String or null

   "ssh_key": String or null
}

PasswordReset 对象

{
   "user_id": TaggedID

   "is_executing": Bool (true if any service password reset is executing)
   "services": {
      ServicePasswordResetObject,
      ServicePasswordResetObject,
      ...
   }
}

ServicePasswordReset 对象

{
   "service_id": String
   "user_id": AnyObject (The type of the id of the service)

   "is_executing": Bool
   "error": Error or null
}

端点

POST /auth/login

Description: Retrieve a new access token.

Parameters:
   username: String (Must be a valid *LDAP DN*)
   password: String

Returns an object with the following properties:
   expiration_date: String (Always a valid ISO 8601 Date)
   token: String
   is_admin: Bool

POST /auth/logout

Description: Revoke an access token.

Returns: The string "ok".

Note: Currently the logout does not do anything. It might in the future
actually disable the token.

GET /api/users/[:officectl_user_id]

Description: List all users in the LDAP, or fetch a specific user. Only an
admin is allowed to list the users. Normal users are only allowed to fetch
themselves.

Returns a User, or a collection of User.

GET /api/password-resets/[:officectl_user_id]

Description: List all password resets in progress. Only an admin is
allowed to list the resets. Normal users are only allowed to fetch the
reset concerning their own account.

Returns a PasswordReset, or a collection of PasswordReset.

PUT /api/password-resets/:officectl_user_id

Description: Create a new password reset. Only admins are allowed to reset
the password of somebody else than themselves and without specifying the
current password.
If a password reset was already in progress for the given user, the call
will fail.

Parameters:
   old_password: String or null
   new_password: String

Returns a PasswordReset.

DELETE /api/password-resets/:officectl_user_id

Description: Delete a password reset.

Returns: The string "ok".

在 macOS 上编译

对于 macOS

swift build [-c release]

直接构建仓库。 如果您想避免大量的 LDAP 相关警告,可以运行一次 ./Scripts/configure.sh。 有关更多信息,请参阅 Package.swift 文件。

对于 Linux

docker build .

项目结构

这是一个标准的 SPM 项目,因此源代码位于 Sources 文件夹中,然后每个源文件位于以其所属目标命名的文件夹中。 测试位于 Tests 文件夹中。

OfficeKit 目标

包含构建 officectl 命令行工具所用的库。

模型

模型的“OfficeKit”部分用于表示 OfficeKit 直接使用的对象。

LDAP 模型包含通用的 LDAPObject 结构和一些实用程序。 它还包含一些类,这些类匹配 LDAP 模式 RFC1274 (cosine) + RFC2798 (inetOrgPerson) 以及 OpenLDAP 中的“core”模式。

其他模型通常都很简单,直接用于存储来自不同 API 的结果。

连接器 & 身份验证器

这些是负责连接或验证不同服务的类。

连接器负责“创建到给定服务的连接”。 例如,对于 LDAP 服务,连接器将创建套接字以连接到 LDAP 服务器。 对于 REST 连接器(例如 GitHub),连接器将生成用于验证向此服务发出的请求的令牌。

身份验证器负责“验证请求”。 例如,GitHub 身份验证器将在 URLRequest 中添加必需的 HTTP 标头。

一个对象可以同时是连接器和身份验证器。 例如,GitHubJWTConnector 就是两者兼具。

操作

它们是标准的 Foundation 的 Operation。 有关更多信息,请参见下文,否则您可以跳过此节。

一个操作代表一个单独的工作单元,同步或异步。 该工作只能执行一次。 配置可以在初始化时完成,也可以在初始化之后,但在操作开始之前完成。 如何检索操作结果没有规则; 通常,操作会存储结果,并在操作结束后检索它。

通常,您会希望在 OperationQueue 中启动一个 Operation,这允许操作具有优先级和依赖关系。 队列将根据这些属性以正确的顺序启动操作。 在队列中启动 Operation 对于同步操作尤为重要:您可能不希望阻塞当前线程直到操作完成!

动作

动作就像操作一样,因为它们都代表一个单独的工作单元。

与操作不同,动作始终是异步的,并且可以重试。

此外,动作也是 SemiSingleton。 这意味着您必须通过 SemiSingletonStore 实例化它们,并且有可能从商店中检索已在执行的动作。 这样做是为了避免同时启动两个执行相同操作的动作。

例如,假设我们有一个重置密码的动作。 我们为用户 A 实例化 ResetPasswordAction 并启动重置。 我们可以为用户 B 实例化一个新的动作,但是如果我们尝试为用户 A 实例化该动作,我们将获得已经启动的那个。

officectl 目标

这是 officectl 可执行文件。 它具有一个命令行,可用于启动 officectl 服务器。

命令

这些包含直接从命令行调用的函数。 要搜索运行 officectl backup mails 时调用的函数,您需要转到 root/backup/mails.swift 文件。

可用命令行动作和参数的配置在 guaka_config.swift 文件中完成。

服务器

“Server”文件夹包含 Web 服务器的控制器。

路由的配置在 setup_routes.swift 文件中完成。

main

一个典型的 Vapor main 文件,除了 CLI 参数解析是通过 guaka 完成的。 您想要做的一切都将在 configure.swift 文件中完成; 特别是,服务的注册和中间件的注册都在这里完成。