private IntPtr HandleMessages(IntPtr handle, int message, IntPtr wParameter, IntPtr lParameter, ref Boolean handled) { var data = Win32Api.GetMessage(message, lParameter); if (data != null) { if (Application.Current.MainWindow == null) { return(IntPtr.Zero); } if (Application.Current.MainWindow.WindowState == WindowState.Minimized) { Application.Current.MainWindow.WindowState = WindowState.Normal; } if (this.ShouldBringToFront) { Win32Api.ActivateWindow(Application.Current.MainWindow.GetWindowHandle()); } var args = data.Split('\n'); if (this.UrlProtocolNames != null && this.UrlProtocolNames.Length > 0 && args.Length > 0 && this.IsUrlProtocol(args)) { try { if (!WebAuthenticationBrokerWrapper.CallBack(args[0])) { this.OnActivationProtocol(new Uri(args[0])); } } catch (Exception) { throw; } } else if (this.IsSingleInstance) { this.OnActivated(args); } handled = true; } return(IntPtr.Zero); }
/// <summary> /// Tries to authenticate the application using OAuth2 to CREST. The application requires a callback. /// Check http://eveonline-third-party-documentation.readthedocs.io/en/latest/sso/nonbrowserapps.html for more information /// </summary> /// <param name="crest">The information required for the authentification</param> /// <exception cref="ArgumentNullException"><paramref name="crest"/> is null</exception> /// <exception cref="ArgumentException">No access scope defined</exception> public async Task AuthorizeApplicationForCrestAsync(CrestApiKey crest, params string[] accessScopes) { if (crest == null) { throw new ArgumentNullException(nameof(crest)); } if (accessScopes.Length == 0) { throw new ArgumentException("At least one access scope must be defined"); } var url = $"{this.Server.Login}oauth/authorize/?response_type=code&redirect_uri={WebUtility.HtmlEncode(crest.CallbackUrl)}&client_id={crest.ClientId}&scope={WebUtility.HtmlEncode(accessScopes.Join(" "))}&state={crest.KeyId}"; var result = await WebAuthenticationBrokerWrapper.AuthenticateAsync(new Uri(url), new Uri(crest.CallbackUrl)); var parameters = new Uri(result).ParseQueryString(); var basicAuth = $"{crest.ClientId}:{crest.SecretKey}".ToBase64String(); var client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", basicAuth); client.DefaultRequestHeaders.Host = "login.eveonline.com"; var response = await client.PostAsync( $"{this.Server.Login}oauth/token/", new FormUrlEncodedContent(new Dictionary <string, string> { { "grant_type", "authorization_code" }, { "code", parameters["code"] } })); var tokenInfo = JsonConvert.DeserializeObject <CrestTokenInfo>(await response.Content.ReadAsStringAsync()); var characterInfo = await WebServiceConsumer.Consume <DefaultRestClient, JsonDeserializer, CrestCharacterInfo>($"{this.Server.Login}oauth/verify", tokenInfo.AccessToken, "login.eveonline.com"); tokenInfo.Authentification = basicAuth; if (this.tokenInfos.Any(x => x.CharacterId == characterInfo.CharacterId)) { this.tokenInfos.RemoveAll(x => x.CharacterId == characterInfo.CharacterId); } this.tokenInfos.Add(new TokenKey { AccessScope = accessScopes, CharacterId = characterInfo.CharacterId, TokenInfo = tokenInfo, ServerName = this.Server.ServerName }); }