async Task<TokenInfo> IDeviceOAuth2Stepwise.WaitForUserConsent(AuthInfo info, CancellationToken cancelToken) { return await PollForUserAuth(info, cancelToken); }
private void OnPromptUser(AuthInfo info) { Debug.WriteLine(info.UserCode); var e = PromptUser; if (e != null) { e.BeginInvoke(this, info, null, null); } }
/// <summary> /// Polls the service endpoint until the user gives authorization or the <see cref="AuthInfo.Expiration"/> passes /// </summary> /// <param name="info">The end point to authorize</param> /// <param name="cancelToken">A cancellation token</param> /// <returns>An auth token</returns> /// <exception cref="UnauthorizedAccessException">Thrown when the user denies access</exception> /// <exception cref="TimeoutException">Thrown when the user does not respond in the time alotted by the service</exception> /// <exception cref="DynamicRestClientResponseException">When there is an http failure</exception> async Task <TokenInfo> IDeviceOAuth2Stepwise.WaitForUserConsent(AuthInfo info, CancellationToken cancelToken) => await PollForUserAuth(info, cancelToken);
private async Task<TokenInfo> PollForUserAuth(AuthInfo authInfo, CancellationToken cancelToken) { if (authInfo == null) throw new ArgumentNullException("authInfo"); using (dynamic authEndPoint = new DynamicRestClient(_endPoint.AuthUri)) { // here poll for success while (DateTimeOffset.UtcNow < authInfo.Expiration) { await Task.Delay(authInfo.PollInterval * 1000); // check the oauth token endpoint ot see if access has been authorized yet using (HttpResponseMessage message = await authEndPoint(_endPoint.TokenPath).post(typeof(HttpResponseMessage), cancelToken, client_id: _clientId, client_secret: _clientSecret, code: authInfo.DeviceCode, type: "device_token", grant_type: "http://oauth.net/grant_type/device/1.0")) { // for some reason facebook returns 400 bad request while waiting for authorization from the user if (message.StatusCode != HttpStatusCode.BadRequest) { message.EnsureSuccessStatusCode(); } var tokenResponse = await message.Deserialize<dynamic>(new JsonSerializerSettings()) as IDictionary<string, object>; if (tokenResponse.ContainsKey("access_token")) { return new TokenInfo() { Site = _endPoint.Name, AccessToken = (string)tokenResponse["access_token"], RefreshToken = tokenResponse.ContainsKey("refresh_token") ? (string)tokenResponse["refresh_token"] : null, Expiry = tokenResponse.ContainsKey("expires_in") ? DateTime.UtcNow + TimeSpan.FromSeconds((long)tokenResponse["expires_in"]) : DateTime.MaxValue, Scheme = _endPoint.Scheme }; } tokenResponse.ThrowIfError(); // if we made it this far it inidcates that the rest endpoint is still in a pending state, so go around again } OnWaitingForConfirmation((int)(authInfo.Expiration - DateTimeOffset.UtcNow).TotalSeconds); } throw new TimeoutException("Access timeout expired"); } }
async Task <TokenInfo> IDeviceOAuth2Stepwise.WaitForUserConsent(AuthInfo info) { return(await PollForUserAuth(info, CancellationToken.None)); }