/// <summary> /// Makes call for Access Token /// </summary> /// <param name="response">Response string containing 'code' token, used for getting access token</param> /// <returns></returns> private async Task <bool> Authorize(string response) { try { string responseData = response.Substring(response.IndexOf("code")); string[] keyValPairs = responseData.Split('='); string code = keyValPairs[1].Split('&')[0]; string clientId = await AppCredentials.GetAppKey(); string appSecret = await AppCredentials.GetAppSecret(); var request = new OauthTokenRequest(clientId, appSecret, code); var token = await client.Oauth.CreateAccessToken(request); if (token != null) { client.Credentials = new Credentials(token.AccessToken); await SaveToken(token.AccessToken, clientId); } return(true); } catch { return(false); } }
/// <summary> /// Opens OAuth window using WebAuthenticationBroker class and returns true is authentication is successful /// </summary> /// <returns></returns> public async Task <bool> Authenticate() { try { string clientId = await AppCredentials.GetAppKey(); OauthLoginRequest request = new OauthLoginRequest(clientId) { Scopes = { "user", "repo" }, }; Uri oauthLoginUrl = client.Oauth.GetGitHubLoginUrl(request); WebAuthenticationResult res = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, oauthLoginUrl, endUri); if (res.ResponseStatus == WebAuthenticationStatus.Success) { var response = res.ResponseData; return(await Authorize(response)); } else { return(false); } } catch { return(false); } }
/// <summary> /// Gets Access token if stored in device /// </summary> /// <returns></returns> public static async Task <string> GetToken() { try { string clientId = await AppCredentials.getAppKey(); var vault = new PasswordVault(); var credentialList = vault.FindAllByResource(clientId); if (credentialList.Count > 0) { credentialList[0].RetrievePassword(); return(credentialList[0].Password); } else { return(null); } } catch { return(null); } }
/// <summary> /// Deletes the access token from user's device /// </summary> /// <returns></returns> public static async Task <bool> signOut() { try { string clientId = await AppCredentials.getAppKey(); var vault = new PasswordVault(); var credentialList = vault.FindAllByResource(clientId); if (credentialList.Count > 0) { vault.Remove(credentialList[0]); } return(true); } catch { return(false); } }