-
Notifications
You must be signed in to change notification settings - Fork 5
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(KNO-7528): add MsTeamsAuthButton #339
Changes from all commits
110ae58
d6307e1
31860d9
2fb7237
4db1c17
1fc1dc8
6a66f81
d5f9ddb
6c66cfd
e5a0658
15909a7
3748edd
94f189a
ccc2794
964d2b0
dc5b72a
c661d85
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@knocklabs/react-core": minor | ||
"@knocklabs/client": minor | ||
"@knocklabs/react": minor | ||
--- | ||
|
||
feat: add MsTeamsAuthButton |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { ApiResponse } from "../../api"; | ||
import { AuthCheckInput, RevokeAccessTokenInput } from "../../interfaces"; | ||
import Knock from "../../knock"; | ||
import { TENANT_OBJECT_COLLECTION } from "../objects/constants"; | ||
|
||
class MsTeamsClient { | ||
private instance: Knock; | ||
|
||
constructor(instance: Knock) { | ||
this.instance = instance; | ||
} | ||
|
||
async authCheck({ tenant: tenantId, knockChannelId }: AuthCheckInput) { | ||
const result = await this.instance.client().makeRequest({ | ||
method: "GET", | ||
url: `/v1/providers/ms-teams/${knockChannelId}/auth_check`, | ||
params: { | ||
ms_teams_tenant_object: { | ||
object_id: tenantId, | ||
collection: TENANT_OBJECT_COLLECTION, | ||
}, | ||
channel_id: knockChannelId, | ||
}, | ||
}); | ||
|
||
return this.handleResponse(result); | ||
} | ||
|
||
async revokeAccessToken({ | ||
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. Again, there’s no way to actually revoke an access token used with the Microsoft Graph API, but calling this function |
||
tenant: tenantId, | ||
knockChannelId, | ||
}: RevokeAccessTokenInput) { | ||
const result = await this.instance.client().makeRequest({ | ||
method: "PUT", | ||
url: `/v1/providers/ms-teams/${knockChannelId}/revoke_access`, | ||
params: { | ||
ms_teams_tenant_object: { | ||
object_id: tenantId, | ||
collection: TENANT_OBJECT_COLLECTION, | ||
}, | ||
channel_id: knockChannelId, | ||
}, | ||
}); | ||
|
||
return this.handleResponse(result); | ||
} | ||
|
||
private handleResponse(response: ApiResponse) { | ||
if (response.statusCode === "error") { | ||
if (response.error?.response?.status < 500) { | ||
return response.error || response.body; | ||
} | ||
throw new Error(response.error || response.body); | ||
} | ||
|
||
return response.body; | ||
} | ||
} | ||
|
||
export default MsTeamsClient; |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -1,14 +1,9 @@ | ||||
import { ApiResponse } from "../../api"; | ||||
import { AuthCheckInput, RevokeAccessTokenInput } from "../../interfaces"; | ||||
import Knock from "../../knock"; | ||||
import { TENANT_OBJECT_COLLECTION } from "../objects/constants"; | ||||
|
||||
import { | ||||
AuthCheckInput, | ||||
GetSlackChannelsInput, | ||||
GetSlackChannelsResponse, | ||||
RevokeAccessTokenInput, | ||||
} from "./interfaces"; | ||||
|
||||
const TENANT_COLLECTION = "$tenants"; | ||||
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. We can replace this with the existing
|
||||
import { GetSlackChannelsInput, GetSlackChannelsResponse } from "./interfaces"; | ||||
|
||||
class SlackClient { | ||||
private instance: Knock; | ||||
|
@@ -24,7 +19,7 @@ class SlackClient { | |||
params: { | ||||
access_token_object: { | ||||
object_id: tenant, | ||||
collection: TENANT_COLLECTION, | ||||
collection: TENANT_OBJECT_COLLECTION, | ||||
}, | ||||
channel_id: knockChannelId, | ||||
}, | ||||
|
@@ -45,7 +40,7 @@ class SlackClient { | |||
params: { | ||||
access_token_object: { | ||||
object_id: tenant, | ||||
collection: TENANT_COLLECTION, | ||||
collection: TENANT_OBJECT_COLLECTION, | ||||
}, | ||||
channel_id: knockChannelId, | ||||
query_options: { | ||||
|
@@ -68,7 +63,7 @@ class SlackClient { | |||
params: { | ||||
access_token_object: { | ||||
object_id: tenant, | ||||
collection: TENANT_COLLECTION, | ||||
collection: TENANT_OBJECT_COLLECTION, | ||||
}, | ||||
channel_id: knockChannelId, | ||||
}, | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,16 +17,6 @@ export type GetSlackChannelsInput = { | |
}; | ||
}; | ||
|
||
export type AuthCheckInput = { | ||
tenant: string; | ||
knockChannelId: string; | ||
}; | ||
|
||
export type RevokeAccessTokenInput = { | ||
tenant: string; | ||
knockChannelId: string; | ||
}; | ||
Comment on lines
-20
to
-28
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. I’m moving these to |
||
|
||
export type GetSlackChannelsResponse = { | ||
slack_channels: SlackChannel[]; | ||
next_cursor: string | null; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from "./modules/core"; | ||
export * from "./modules/feed"; | ||
export * from "./modules/ms-teams"; | ||
export * from "./modules/slack"; | ||
export * from "./modules/i18n"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import * as React from "react"; | ||
import { PropsWithChildren } from "react"; | ||
|
||
import { useKnockClient } from "../../core"; | ||
import { msTeamsProviderKey } from "../../core/utils"; | ||
import { useMsTeamsConnectionStatus } from "../hooks"; | ||
import { ConnectionStatus } from "../hooks/useMsTeamsConnectionStatus"; | ||
|
||
export interface KnockMsTeamsProviderState { | ||
knockMsTeamsChannelId: string; | ||
tenantId: string; | ||
connectionStatus: ConnectionStatus; | ||
setConnectionStatus: (connectionStatus: ConnectionStatus) => void; | ||
errorLabel: string | null; | ||
setErrorLabel: (label: string) => void; | ||
actionLabel: string | null; | ||
setActionLabel: (label: string | null) => void; | ||
} | ||
|
||
const MsTeamsProviderStateContext = | ||
React.createContext<KnockMsTeamsProviderState | null>(null); | ||
|
||
export interface KnockMsTeamsProviderProps { | ||
knockMsTeamsChannelId: string; | ||
tenantId: string; | ||
} | ||
|
||
export const KnockMsTeamsProvider: React.FC< | ||
PropsWithChildren<KnockMsTeamsProviderProps> | ||
> = ({ knockMsTeamsChannelId, tenantId, children }) => { | ||
const knock = useKnockClient(); | ||
|
||
const { | ||
connectionStatus, | ||
setConnectionStatus, | ||
errorLabel, | ||
setErrorLabel, | ||
actionLabel, | ||
setActionLabel, | ||
} = useMsTeamsConnectionStatus(knock, knockMsTeamsChannelId, tenantId); | ||
|
||
return ( | ||
<MsTeamsProviderStateContext.Provider | ||
key={msTeamsProviderKey({ | ||
knockMsTeamsChannelId, | ||
tenantId, | ||
connectionStatus, | ||
errorLabel, | ||
})} | ||
value={{ | ||
connectionStatus, | ||
setConnectionStatus, | ||
errorLabel, | ||
setErrorLabel, | ||
actionLabel, | ||
setActionLabel, | ||
knockMsTeamsChannelId, | ||
tenantId, | ||
}} | ||
> | ||
{children} | ||
</MsTeamsProviderStateContext.Provider> | ||
); | ||
}; | ||
|
||
export const useKnockMsTeamsClient = (): KnockMsTeamsProviderState => { | ||
const context = React.useContext(MsTeamsProviderStateContext); | ||
if (!context) { | ||
throw new Error( | ||
"useKnockMsTeamsClient must be used within a KnockMsTeamsProvider", | ||
); | ||
} | ||
return context; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./KnockMsTeamsProvider"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default as useMsTeamsConnectionStatus } from "./useMsTeamsConnectionStatus"; | ||
export { default as useMsTeamsAuth } from "./useMsTeamsAuth"; |
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.
Personally, I find this
yarn run dev:packages
command useful, but I’m happy to remove it if others don’t find it useful.