public Task SaveRefreshTokenAsync(RefreshTokenInfo tokenInfo) { return Task.Factory.StartNew(() => { _configurationService.SetValue("RefreshToken", tokenInfo.RefreshToken); }); }
public Task SaveRefreshTokenAsync(RefreshTokenInfo tokenInfo) { // Note: // 1) In order to receive refresh token, wl.offline_access scope is needed. // 2) Alternatively, we can persist the refresh token. return Task.Factory.StartNew(() => { if (File.Exists(path+"OneDrive.cloudmanager")) File.Delete(path+"OneDrive.cloudmanager"); if (!Directory.Exists(Path.GetDirectoryName(path+"OneDrive.cloudmanager"))) Directory.CreateDirectory(Path.GetDirectoryName(path+"OneDrive.cloudmanager")); File.AppendAllText(path+"OneDrive.cloudmanager", tokenInfo.RefreshToken); }); }
private void UpdateSession(LiveLoginResult result) { Debug.Assert(result != null); if (result.Session != null) { // Set the AuthClient that is needed when refreshing a token. result.Session.AuthClient = this.publicAuthClient; // We have a new session, update the public property this.loginStatus = result; this.publicAuthClient.Session = result.Session; if (this.refreshTokenHandler != null && !string.IsNullOrEmpty(result.Session.RefreshToken)) { RefreshTokenInfo refreshInfo = new RefreshTokenInfo(result.Session.RefreshToken); this.refreshTokenHandler.SaveRefreshTokenAsync(refreshInfo); } } else if (this.loginStatus.Status == LiveConnectSessionStatus.Unknown && result.Status == LiveConnectSessionStatus.NotConnected) { this.loginStatus = result; } }
internal void TryRefreshToken(Action<LiveLoginResult> completionCallback) { LiveLoginResult result = new LiveLoginResult(LiveConnectSessionStatus.Unknown, null); if (this.refreshTokenHandler != null) { if (this.refreshTokenInfo == null) { this.refreshTokenHandler.RetrieveRefreshTokenAsync().ContinueWith(t => { this.refreshTokenInfo = t.Result; this.RefreshToken(completionCallback); }); return; } this.RefreshToken(completionCallback); return; } this.OnRefreshTokenCompleted(result, completionCallback); }
private void TryRefreshToken(string redirectUrl) { Debug.Assert(this.loginStatus != null); IEnumerable<string> scopes; LiveAuthException error; bool isTokenRequest = this.CheckRefreshTokenRequest(out scopes, out error); if (error != null) { this.OnAuthTaskCompleted(new LiveLoginResult(error)); return; } // Try to refresh a token if // i) there is a token request or // ii) we don't have a token or // iii) the current token is expired. LiveLoginResult result = null; LiveConnectSession session = this.loginStatus.Session; bool hasValidToken = session != null && session.IsValid; bool shouldRefresh = (this.refreshTokenHandler != null) && (isTokenRequest || !hasValidToken); if (!shouldRefresh) { this.OnAuthTaskCompleted(null); return; } if (this.initScopes == null) { // We don't have initScopes, then use the scopes received from Url. this.initScopes = scopes; } this.refreshTokenHandler.RetrieveRefreshTokenAsync().ContinueWith(t => { try { this.refreshTokenInfo = t.Result; if (this.refreshTokenInfo != null) { string currentUserId = this.publicAuthClient.CurrentUserId; if (currentUserId != null && this.refreshTokenInfo.UserId != currentUserId) { // There is a user Id available in current session. We need to ensure the token provided matches it. result = new LiveLoginResult(new LiveAuthException( AuthErrorCodes.InvalidRequest, ErrorText.RefereshTokenNotMatchUserId)); } else { LiveAuthRequestUtility.RefreshTokenAsync( this.clientId, this.clientSecret, redirectUrl, this.refreshTokenInfo.RefreshToken, null/*scopes - We intentially specify null scopes and validate the initScopes after we have the session * result. With this approach, we can return notConnected if initScopes is not satisfied, and surface * the error if there is one. */ ).ContinueWith((Task<LiveLoginResult> rt) => { result = rt.Result; this.OnAuthTaskCompleted(result); }); return; } } } catch (Exception ex) { error = new LiveAuthException(AuthErrorCodes.ClientError, ErrorText.RetrieveRefreshTokenError, ex); result = new LiveLoginResult(error); } this.OnAuthTaskCompleted(result); }); }
private void OnAuthTaskCompleted( LiveLoginResult loginResult) { if (loginResult != null) { if (loginResult.Session != null) { LiveAuthException error = this.ValidateSession(loginResult.Session); if (error != null) { loginResult = new LiveLoginResult(error); } else { // We have a new session, update the LiveAuthClient.Session this.loginStatus = loginResult; if (this.refreshTokenHandler != null && !string.IsNullOrEmpty(loginResult.Session.RefreshToken)) { string userId; if (this.GetUserId(loginResult.Session.AuthenticationToken, out userId, out error)) { RefreshTokenInfo refreshInfo = new RefreshTokenInfo(loginResult.Session.RefreshToken, userId); Task saveTokenTask = this.refreshTokenHandler.SaveRefreshTokenAsync(refreshInfo); saveTokenTask.ContinueWith((tk) => { this.CompleteAuthTask(loginResult); }); return; } else { loginResult = new LiveLoginResult(error); } } } } } else { // We should return the existing status for cases like already initialized or can't refresh ticket. loginResult = this.loginStatus; } this.CompleteAuthTask(loginResult); }
Task IRefreshTokenHandler.SaveRefreshTokenAsync(RefreshTokenInfo tokenInfo) { // Note: // 1) In order to receive refresh token, wl.offline_access scope is needed. // 2) Alternatively, we can persist the refresh token. return Task.Factory.StartNew(() => { this.refreshTokenInfo = tokenInfo; }); }