-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '2806' of https://github.com/tdurnford/BotFramework-WebChat
- Loading branch information
Showing
2 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
...on-behalf-of-authentication/app/src/ui/WebChat/Attachments/OnBehalfOfAttachment/index.css
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,71 @@ | ||
.button-container { | ||
display: flex; | ||
flex: 1; | ||
position: relative; | ||
} | ||
|
||
.card { | ||
font-family: inherit; | ||
padding: 10px; | ||
} | ||
|
||
.card > .buttons { | ||
align-items: stretch; | ||
display: flex; | ||
flex-direction: column; | ||
} | ||
|
||
.card > .buttons > .button-container > button { | ||
flex: 1; | ||
color: #0063B1; | ||
background-color: white; | ||
border-style: solid; | ||
border-width: 1px; | ||
font-weight: 600; | ||
padding: 10px | ||
} | ||
|
||
.card > .buttons > .button-container:last-of-type { | ||
margin-top: 8px; | ||
} | ||
|
||
.container { | ||
width: 10px; | ||
text-align: center; | ||
} | ||
|
||
.status { | ||
position: absolute; | ||
top: 9px; | ||
right: 9px; | ||
} | ||
|
||
.separator { | ||
height: 8px; | ||
} | ||
|
||
/* | ||
Your use of the content in the files referenced here is subject to the terms of the license at https://aka.ms/fabric-assets-license | ||
*/ | ||
@font-face { | ||
font-family: 'FabricMDL2Icons'; | ||
src: url('data:application/octet-stream;base64,d09GRgABAAAAAAi4AA4AAAAAEIgAA6PXAAAAAAAAAAAAAAAAAAAAAAAAAABPUy8yAAABRAAAAEgAAABgOcN1TGNtYXAAAAGMAAAAMgAAAUIADfEjY3Z0IAAAAcAAAAAgAAAAKgnZCa9mcGdtAAAB4AAAAPAAAAFZ/J7mjmdhc3AAAALQAAAADAAAAAwACAA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SnVSsu42/kvctMjho5/he4l5iTprZyv4F88RzQOlDmOHT8ZOhv4/tsxRnhrhnLyX7seihR+mCvVG1s63RPedssSOa+yhWFtT7V1yTYCZ6fLfMXWQiMjZok8xw1Q41qA4FU9wXrGe54z1LiA+KY7kz0TZPXlTaS/Ztua9Uc+AzsppzHkMnaq6IrIa8ooXnLrgNyWJVw/RSXbW8L9Amx/2U+YozH+NOV3E+rkDzJHbMU47rds66VClp51hNy3NXbOWebGpGB6h/C980ofPEyzbvMYf/yu3ae8GeSpQ5nuOQ7tQwq9sqGKJv5vXo3AxQJbGWwPGGW0D+Y60FSjqu3PCt/NTsyQtTpbgvJq2xqohbvlktW1K2QzcHP6RZ803+5xmNX8YmdWbtfbghOrFM80P5zpnp2Nv/4W7/DV7sOJR4nGNgZgCD/34M5QyYgAkAKSwBynic28CgzbCJkZNJm3ETF4jcztWaG2qrysChvZ07NdhBTwbE4onwsNCQBLF4nc215YVBLD4dFRkRHhCLX05CmI8DxBLg4+FkZwGxBMEAxBLaMKEgwADIYtjOCDeaCW40M9xoFrjRrHCj2eQkoUazw43mgBvNCTd6kzAju/YGBgXX2kwJFwDEASgaAAAA') format('truetype'); | ||
} | ||
|
||
.ms-Icon { | ||
-moz-osx-font-smoothing: grayscale; | ||
-webkit-font-smoothing: antialiased; | ||
display: inline-block; | ||
font-family: 'FabricMDL2Icons'; | ||
font-style: normal; | ||
font-weight: normal; | ||
speak: none; | ||
} | ||
|
||
.ms-Icon--SkypeCircleCheck:before { | ||
color: green; | ||
content: "\EF7D"; | ||
font-size: 16px; | ||
padding-top: 4; | ||
} | ||
|
112 changes: 112 additions & 0 deletions
112
...-on-behalf-of-authentication/app/src/ui/WebChat/Attachments/OnBehalfOfAttachment/index.js
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,112 @@ | ||
import React, { useCallback, useContext, useEffect, useMemo, useState, useRef } from 'react'; | ||
import { hooks } from 'botframework-webchat'; | ||
import { Spinner } from 'office-ui-fabric-react/lib/Spinner'; | ||
|
||
import './index.css'; | ||
import OAuthContext from '../../../../oauth/Context'; | ||
|
||
const { useActivities, usePostActivity } = hooks; | ||
|
||
const AUTHENTICATING = 'AUTHENTICATING'; | ||
const AUTHENTICATION_FAILED = 'AUTHENTICATION_FAILED'; | ||
const AUTHENTICATION_PENDING = 'AUTHENTICATION_PENDING'; | ||
const AUTHENTICATION_SUCCESSFUL = 'AUTHENTICATION_SUCCESSFUL'; | ||
|
||
const OnBehalfOfAttachment = ({ card, next }) => { | ||
const { | ||
attachment: { | ||
content: { connectionName, tokenExchangeResource: { id, uri } = {} } | ||
} | ||
} = card; | ||
const { acquireToken, getAccount } = useContext(OAuthContext); | ||
const [authenticationStatus, setAuthenticationStatus] = useState(AUTHENTICATION_PENDING); | ||
const { current: invokeId } = useRef( | ||
Math.random() | ||
.toString(36) | ||
.substr(2, 10) | ||
); | ||
const oauthCard = useMemo(() => next(card), [next, card]); | ||
|
||
const [activities] = useActivities(); | ||
const postActivity = usePostActivity(); | ||
|
||
const exchangeToken = useCallback( | ||
async resourceUri => { | ||
const user = getAccount(); | ||
if (user) { | ||
const { accessToken } = await acquireToken({ scopes: [resourceUri] }); | ||
return accessToken; | ||
} | ||
setAuthenticationStatus(AUTHENTICATION_FAILED); | ||
}, | ||
[acquireToken, getAccount] | ||
); | ||
|
||
useEffect(() => { | ||
const invokeActivity = activities.find( | ||
({ channelData: { invokeId: activityInvokeId } = {} }) => invokeId === activityInvokeId | ||
); | ||
if (invokeActivity) { | ||
const { channelData: { state } = {} } = invokeActivity; | ||
if (state === 'send failed') { | ||
setAuthenticationStatus(AUTHENTICATION_FAILED); | ||
} else if (state === 'sent') { | ||
setAuthenticationStatus(AUTHENTICATION_SUCCESSFUL); | ||
} | ||
} | ||
}, [activities, invokeId]); | ||
|
||
useEffect(() => { | ||
if (authenticationStatus === AUTHENTICATING) { | ||
(async function() { | ||
try { | ||
const token = await exchangeToken('api://61598522-abf1-49ba-bbb4-3fb89f4ad9a6/ReadUser'); | ||
token && | ||
postActivity({ | ||
channelData: { invokeId }, | ||
type: 'invoke', | ||
name: 'signin/tokenExchange', | ||
value: { | ||
id, | ||
connectionName, | ||
token | ||
} | ||
}); | ||
} catch (error) { | ||
setAuthenticationStatus(AUTHENTICATION_FAILED); | ||
} | ||
})(); | ||
} | ||
}, [authenticationStatus]); | ||
|
||
const handleClickAllow = useCallback(() => { | ||
authenticationStatus !== AUTHENTICATING && setAuthenticationStatus(AUTHENTICATING); | ||
}, [authenticationStatus, setAuthenticationStatus]); | ||
|
||
if (authenticationStatus === AUTHENTICATION_FAILED) { | ||
return oauthCard; | ||
} | ||
|
||
return ( | ||
<div className="card"> | ||
<div>The Bot has requested to access your account</div> | ||
<div className="separator" /> | ||
<div className="ac-actionSet buttons"> | ||
<div className="button-container"> | ||
<button className="ac-pushButton style-default" type="button" onClick={handleClickAllow}> | ||
Allow | ||
</button> | ||
<div className="status"> | ||
{authenticationStatus === AUTHENTICATING ? ( | ||
<Spinner /> | ||
) : authenticationStatus === AUTHENTICATION_SUCCESSFUL ? ( | ||
<i className="ms-Icon ms-Icon--SkypeCircleCheck" aria-hidden="true" /> | ||
) : null} | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default OnBehalfOfAttachment; |