Skip to content
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

feat(connector): add Xiaomi social login connector #6905

Merged
merged 5 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rotten-lizards-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@logto/connector-xiaomi": minor
---

add Xiaomi social connector
69 changes: 69 additions & 0 deletions packages/connectors/connector-xiaomi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Xiaomi social connector

The official Logto connector for Xiaomi social sign-in. [中文文档](https://github.com/logto-io/logto/tree/master/packages/connectors/connector-xiaomi/README.zh-CN.md)

**Table of contents**

- [Xiaomi social connector](#xiaomi-social-connector)
- [Get started](#get-started)
- [Configure Xiaomi OAuth application](#configure-xiaomi-oauth-application)
- [Scopes description](#scopes-description)
- [Test Xiaomi connector](#test-xiaomi-connector)
- [References](#references)

## Get started

1. Create a developer account at [Xiaomi Open Platform](https://dev.mi.com/)
2. Visit [Xiaomi Account Service](https://dev.mi.com/passport/oauth2/applist)
3. Create a new application if you don't have one

## Configure Xiaomi OAuth application

1. Visit [Xiaomi Account Service](https://dev.mi.com/passport/oauth2/applist)
2. Configure OAuth settings:
- Open the application you want to use for login, click on "Callback URL" (if you haven't edited the callback URL, it will display as "Enabled")
- Add authorization callback URL: `${your_logto_origin}/callback/${connector_id}`
- `connector_id` can be found on the top of the connector details page in Logto Console
3. Get `AppID` and `AppSecret` from the application details page
4. Fill in the `clientId` and `clientSecret` fields in Logto Console with the values from step 3
5. Optional configuration:
- `skipConfirm`: Whether to skip the Xiaomi authorization confirmation page when user is already logged in to Xiaomi account, defaults to false

## Scopes description

By default, the connector requests the following scope:

- `1`: Read user profile

Available scopes:

| Scope Value | Description | API Interface |
|-------------|-------------|---------------|
| 1 | Get user profile | user/profile |
| 3 | Get user open_id | user/openIdV2 |
| 1000 | Get Xiaomi router info | Mi Router |
| 1001 | Access all Xiaomi router info | Mi Router |
| 2001 | Access Xiaomi cloud calendar | Mi Cloud |
| 2002 | Access Xiaomi cloud alarm | Mi Cloud |
| 6000 | Use Mi Home smart home service | Mi Home |
| 6002 | Add third-party devices to Mi Home | Mi Home |
| 6003 | Alexa control Xiaomi devices | Mi Home |
| 6004 | Third-party service access to Xiaomi devices | Mi Home |
| 7000 | Follow Yellow Pages service account | Mi Cloud |
| 11000 | Get Xiaomi cloud photos | Mi Cloud |
| 12001 | Save app data to Mi Cloud | Mi Cloud |
| 12005 | Use health ECG service | Health |
| 16000 | Get Mi Wallet passes | app/get_pass |
| 20000 | Enable XiaoAI voice service | XiaoAI |
| 40000 | Enable cloud AI service | Internal Use |

Multiple scopes can be configured by separating them with spaces, e.g.: `1 3 6000`.

## Test Xiaomi connector

That's it. Don't forget to [Enable social sign-in](https://docs.logto.io/connectors/social-connectors#enable-social-sign-in) in the sign-in experience.

## References

- [Xiaomi OAuth 2.0 Documentation](https://dev.mi.com/xiaomihyperos/documentation/detail?pId=1708)
- [Xiaomi Get User Profile Documentation](https://dev.mi.com/xiaomihyperos/documentation/detail?pId=1517)
71 changes: 71 additions & 0 deletions packages/connectors/connector-xiaomi/README.zh-CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# 小米社交连接器

小米社交登录 Logto 官方连接器 [中文文档](#小米社交连接器)

**目录**

- [小米社交连接器](#小米社交连接器)
- [开始上手](#开始上手)
- [配置小米 OAuth 应用](#配置小米-oauth-应用)
- [权限范围说明](#权限范围说明)
- [测试小米连接器](#测试小米连接器)
- [参考](#参考)

小米是一家全球知名的科技公司,提供包括智能手机、智能家居等在内的多种产品和服务。本连接器可以帮助终端用户使用小米账号登录你的应用。

## 开始上手

1. 在[小米开放平台](https://dev.mi.com/)创建开发者账号
2. 访问[小米帐号服务](https://dev.mi.com/passport/oauth2/applist)
3. 创建一个新应用(如果还没有)

## 配置小米 OAuth 应用

1. 访问[小米帐号服务](https://dev.mi.com/passport/oauth2/applist)
2. 配置 OAuth 设置:
- 打开要用于登录的应用,点击「回调地址」(如果没有编辑过回调地址,会显示为「启用」)
- 添加授权回调地址: `${your_logto_origin}/callback/${connector_id}`
- `connector_id` 可以在 Logto 管理控制台连接器详情页顶部找到
3. 从应用详情页获取 `AppID` 和 `AppSecret`
4. 将第 3 步获取的值填入 Logto 管理控制台的 `clientId` 和 `clientSecret` 字段
5. 可选配置:
- `skipConfirm`: 在用户已登录小米账号的情况下,是否跳过小米授权确认页面,默认为 false

## 权限范围说明

默认情况下,连接器请求以下权限:

- `1`: 读取用户资料

可配置的权限范围:

| 权限值 | 描述 | API 接口 |
|-------|------|----------|
| 1 | 获取用户资料 | user/profile |
| 3 | 获取用户 open_id | user/openIdV2 |
| 1000 | 获取小米路由器信息 | 路由器 |
| 1001 | 访问所有小米路由器信息 | 路由器 |
| 2001 | 访问小米云日历 | 小米云 |
| 2002 | 访问小米云闹钟 | 小米云 |
| 6000 | 使用米家智能家居服务 | 米家 |
| 6002 | 添加第三方设备到米家 | 米家 |
| 6003 | Alexa 控制小米设备 | 米家 |
| 6004 | 第三方服务访问小米设备 | 米家 |
| 7000 | 关注黄页服务号 | 小米云 |
| 11000 | 获取小米云相册 | 小米云 |
| 12001 | 保存应用数据到小米云 | 小米云 |
| 12005 | 使用健康心电图服务 | 健康 |
| 16000 | 获取小米卡包卡券 | app/get_pass |
| 20000 | 启用小爱智能语音服务 | 小爱 |
| 40000 | 启用云端 AI 服务 | 内部使用 |

可以通过空格分隔配置多个权限范围,例如: `1 3 6000`。

## 测试小米连接器

大功告成!别忘了在[登录体验](https://docs.logto.io/zh-CN/connectors/social-connectors#enable-social-sign-in)中启用该连接器。

## 参考

- [小米 OAuth 2.0 文档](https://dev.mi.com/xiaomihyperos/documentation/detail?pId=1708)
- [小米获取用户信息文档](https://dev.mi.com/xiaomihyperos/documentation/detail?pId=1517)
22 changes: 22 additions & 0 deletions packages/connectors/connector-xiaomi/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 changes: 69 additions & 0 deletions packages/connectors/connector-xiaomi/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"name": "@logto/connector-xiaomi",
"version": "1.0.0",
"description": "Xiaomi web connector implementation.",
"author": "Silverhand Inc. <contact@silverhand.io>",
"dependencies": {
"@logto/connector-kit": "workspace:^4.0.0",
"@silverhand/essentials": "^2.9.1",
"ky": "^1.2.3",
"zod": "^3.23.8"
},
"main": "./lib/index.js",
"module": "./lib/index.js",
"exports": "./lib/index.js",
"license": "MPL-2.0",
"type": "module",
"files": [
"lib",
"docs",
"logo.svg",
"logo-dark.svg"
],
"scripts": {
"precommit": "lint-staged",
"check": "tsc --noEmit",
"build": "tsup",
"dev": "tsup --watch",
"lint": "eslint --ext .ts src",
"lint:fix": "eslint --ext .ts src --fix",
"lint:report": "pnpm lint --format json --output-file report.json",
"test": "vitest src",
"test:ci": "pnpm run test --silent --coverage",
"prepublishOnly": "pnpm build"
},
"engines": {
"node": "^20.9.0"
},
"eslintConfig": {
"extends": "@silverhand",
"settings": {
"import/core-modules": [
"@silverhand/essentials",
"got",
"nock",
"snakecase-keys",
"zod"
]
}
},
"prettier": "@silverhand/eslint-config/.prettierrc",
"publishConfig": {
"access": "public"
},
"devDependencies": {
"@silverhand/eslint-config": "6.0.1",
"@silverhand/ts-config": "6.0.0",
"@types/node": "^20.11.20",
"@types/supertest": "^6.0.2",
"@vitest/coverage-v8": "^2.1.8",
"eslint": "^8.56.0",
"lint-staged": "^15.0.2",
"nock": "14.0.0-beta.15",
"prettier": "^3.0.0",
"supertest": "^7.0.0",
"tsup": "^8.3.0",
"typescript": "^5.5.3",
"vitest": "^2.1.8"
}
}
59 changes: 59 additions & 0 deletions packages/connectors/connector-xiaomi/src/constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import type { ConnectorMetadata } from '@logto/connector-kit';
import { ConnectorPlatform, ConnectorConfigFormItemType } from '@logto/connector-kit';

export const authorizationEndpoint = 'https://account.xiaomi.com/oauth2/authorize';
export const accessTokenEndpoint = 'https://account.xiaomi.com/oauth2/token';
export const userInfoEndpoint = 'https://open.account.xiaomi.com/user/profile';

// Default scope is read user profile
export const defaultScope = '1';

export const defaultMetadata: ConnectorMetadata = {
id: 'xiaomi-universal',
target: 'xiaomi',
platform: ConnectorPlatform.Universal,
name: {
en: 'Xiaomi',
'zh-CN': '小米',
},
logo: './logo.svg',
logoDark: null,
description: {
en: 'Xiaomi is a Chinese electronics company.',
'zh-CN': '小米是一家中国的电子产品公司。',
},
readme: './README.md',
formItems: [
{
key: 'clientId',
type: ConnectorConfigFormItemType.Text,
label: 'Client ID',
required: true,
placeholder: '<client-id>',
},
{
key: 'clientSecret',
type: ConnectorConfigFormItemType.Text,
label: 'Client Secret',
required: true,
placeholder: '<client-secret>',
},
{
key: 'scope',
type: ConnectorConfigFormItemType.Text,
label: 'Scope',
required: false,
placeholder: '<scope>',
description: 'The scope determines permissions granted by the user.',
},
{
key: 'skipConfirm',
type: ConnectorConfigFormItemType.Switch,
label: 'Skip Auth Confirm Page',
required: false,
description: 'Skip the Xiaomi auth confirm page when the user is already logged in.',
},
],
};

export const defaultTimeout = 5000;
Loading
Loading