/// <summary> /// Starts the default browser to obtain the AccessCode to be used with ProcessAuthorizationCode() /// </summary> public void SignIn(OAuthSettings oAuthApplication) { var request = new RequestUrl(authorizationUrl); var url = GetSigninURL(oAuthApplication); System.Diagnostics.Process.Start(url); }
/// <summary> /// Uses the Embedded Broswer for login and auto process the AccessCode /// </summary public async Task <BrowserResultType> SignIn(OAuthSettings oAuthApplication, OAuthToken token) { var request = new RequestUrl(authorizationUrl); var url = request.CreateAuthorizeUrl( clientId: oAuthApplication.ClientID, responseType: OidcConstants.ResponseTypes.Code, scope: _scope, state: oAuthApplication.ApplicationID.ToString(), redirectUri: oAuthApplication.ReturnUrl); var Browser = new HMRC.Browser.WinFormsBroswer(); var browserOptions = new BrowserOptions(url, "", SuccessTitle); browserOptions.DisplayMode = DisplayMode.Visible; var browserResult = await Browser.InvokeAsync(browserOptions); if (browserResult.ResultType == BrowserResultType.Success) { var r = browserResult.Response.Split('&'); Dictionary <String, String> p = new Dictionary <string, string>(); foreach (var item in r) { var r1 = item.Split('='); p.Add(r1[0], r1[1]); } string code = p[SuccessTitle]; ProcessAuthorizationCode(code, oAuthApplication, token); } return(browserResult.ResultType); }
/// <summary> /// Process Authorization Code from External Browser and request Token /// </summary> public bool ProcessAuthorizationCode(string code, OAuthSettings application, OAuthToken token) { AuthorizationCodeTokenRequest request = new AuthorizationCodeTokenRequest() { ClientId = application.ClientID, ClientSecret = application.ClientSecret, Code = code, RequestUri = new Uri(tokenUrl), RedirectUri = application.ReturnUrl }; try { using (HttpClient httpClient = new HttpClient()) { Task <TokenResponse> T = Task.Run(() => httpClient.RequestAuthorizationCodeTokenAsync(request)); TokenResponse tokenResponse = T.Result; FillTokenFromTokenResponse(tokenResponse, token); return(true); } } catch (Exception) { return(false); } }
/// <summary> /// Constructor /// </summary> public VATMaint(OAuthSettings hMRCOAuthApplication, Dictionary <string, string> FraudHeaders = null) { if (FraudHeaders == null) { FraudHeaders = new Dictionary <string, string>(); } OAuthToken _oAuthToken; TokenKey = System.Security.Principal.WindowsIdentity.GetCurrent().User.Value.Replace("-", ""); TokenKey = TokenKey.Substring(TokenKey.Length - 24); if (System.IO.File.Exists(@"Token.dat")) { TokenFile = @"Token.dat"; } else { string fldr = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "HMRC", "VAT"); System.IO.Directory.CreateDirectory(fldr); #if DEBUG TokenFile = System.IO.Path.Combine(fldr, @"Token_Debug.dat"); #else TokenFile = System.IO.Path.Combine(fldr, @"Token.dat"); #endif } vrn = hMRCOAuthApplication.VRN; _oAuthApplication = hMRCOAuthApplication; if (System.IO.File.Exists(TokenFile)) { try { System.Xml.Serialization.XmlSerializer reader = new System.Xml.Serialization.XmlSerializer(typeof(OAuthToken)); using (System.IO.StreamReader file = new System.IO.StreamReader(TokenFile)) { _oAuthToken = (OAuthToken)reader.Deserialize(file); file.Close(); _oAuthToken.RefreshToken = Decrypt(_oAuthToken.RefreshToken, TokenKey); _oAuthToken.AccessToken = Decrypt(_oAuthToken.AccessToken, TokenKey); _oAuthToken.Bearer = Decrypt(_oAuthToken.Bearer, TokenKey); } } catch (Exception) { _oAuthToken = new OAuthToken(); } } else { _oAuthToken = new OAuthToken(); } VATProvider = new VATApi(_oAuthApplication, _oAuthToken, vrn, FraudHeaders, SaveOAuthToken); }
/// <summary> /// Created a Login URL incase you want to Call a different Browser from SignIn() /// </summary> public string GetSigninURL(OAuthSettings oAuthApplication) { var request = new RequestUrl(authorizationUrl); var url = request.CreateAuthorizeUrl( clientId: oAuthApplication.ClientID, responseType: OidcConstants.ResponseTypes.Code, scope: _scope, state: oAuthApplication.ApplicationID.ToString(), redirectUri: oAuthApplication.ReturnUrl); //Wish I could send this in! //UserName = oAuthApplication.UserID, //Password = oAuthApplication.Password, return(url); }
public void RefreshAccessToken(OAuthSettings oAuthApplication, OAuthToken token) { if (token.RefreshToken == null) { throw new Exceptions.VATAPIInvalidToken(Model.error.REFRESH_TOKEN_IS_MISSING); } var request = new RefreshTokenRequest() { ClientId = oAuthApplication.ClientID, ClientSecret = oAuthApplication.ClientSecret, RefreshToken = token.RefreshToken, RequestUri = new Uri(tokenUrl) }; using (HttpClient httpClient = new HttpClient()) { Task <TokenResponse> T = Task.Run(() => httpClient.RequestRefreshTokenAsync(request)); TokenResponse tokenResponse = T.Result; if (!tokenResponse.IsError) { FillTokenFromTokenResponse(tokenResponse, token); return; } string error = tokenResponse.Error; string error_description = ""; try { error_description = (((Newtonsoft.Json.Linq.JValue)tokenResponse.Json["error_description"]).Value).ToString(); } catch (Exception) { } Exceptions.VATAPIInvalidToken e = new Exceptions.VATAPIInvalidToken(Model.error.IMPOSSIBLE_TO_REFRESH_TOKEN); e.Data.Add("AccessToken", token.AccessToken); e.Data.Add("RefreshToken", token.RefreshToken); e.Data.Add("Raw", tokenResponse.Raw); throw e; } }
public VATApi(OAuthSettings Application, OAuthToken Token, string VRN, Dictionary <string, string> FraudHeaders, SaveOAuthTokenDelegate SaveOAuthToken = null) { application = Application; token = Token; urlSite = Application.ServerUrl; saveOAuthToken = SaveOAuthToken; setVRN(VRN); fraudHeaders = FraudHeaders; ApplicationProcessor = new MTDApplicationProcessor(Application.ServerUrl); try { if (token != null && token.RefreshToken != null) { RefreshAccessToken(); } } catch (VATAPIInvalidToken) { // if (ex.Code == "asd") // token = new OAuthToken(); } }
public void SignOut(OAuthSettings application, OAuthToken token) { //Nothing to do at API Level }