/// <summary>
        /// Prevents a default instance of the ApplicationEndpoint class from being created
        /// </summary>
        public ApplicationEndpoint(IClientPlatform platform, ApplicationEndpointSettings applicationEndpointSettings, IEventChannel eventChannel)
        {
            ClientPlatform     = platform;
            m_endpointSettings = applicationEndpointSettings;

            if (eventChannel != null)
            {
                m_eventChannel = eventChannel;
                m_eventChannel.HandleIncomingEvents += this.OnReceivedCallback;
            }

            Logger.Instance.Information("Initializing ApplicationEndpoint");

            var oauthTokenIdentitifier = new OAuthTokenIdentifier(
                Constants.PlatformAudienceUri,
                applicationEndpointSettings.ApplicationEndpointId.Domain);

            var tokenProvider = new AADServiceTokenProvider(platform.AADClientId.ToString(), Constants.AAD_AuthorityUri, platform.AADAppCertificate, platform.AADClientSecret);

            if (!platform.IsInternalPartner)
            {
                TokenMapper.RegisterNameSpaceHandling(Constants.DefaultResourceNamespace, Constants.PublicServiceResourceNamespace);
            }

            TokenMapper.RegisterTypesInAssembly(Assembly.GetAssembly(typeof(ApplicationsResource)));
            m_tokenProvider        = tokenProvider;
            m_oauthTokenIdentifier = oauthTokenIdentitifier;

            m_restfulClient = ((ClientPlatform)ClientPlatform).RestfulClientFactory.GetRestfulClient(m_oauthTokenIdentifier, m_tokenProvider);

            Logger.Instance.Information("ApplicationEndpoint Initialized!");
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="OauthEvoRestfulClient"/> class.
        /// </summary>
        /// <param name="tokenProvider">The token provider.</param>
        /// <param name="oauthIdentity">The oauth identity.</param>
        public OauthEvoRestfulClient(ITokenProvider tokenProvider, OAuthTokenIdentifier oauthIdentity)
        {
            if (tokenProvider == null)
            {
                throw new ArgumentNullException(nameof(tokenProvider), "The parameter named tokenProvider can't be null.");
            }

            this.m_tokenProvider = tokenProvider;
            HttpClientHandler handler = new HttpClientHandler();

            handler.AllowAutoRedirect = false;
            m_httpClient         = new HttpClient(handler);
            m_httpClient.Timeout = TimeSpan.FromSeconds(30);
            m_oauthIdentity      = oauthIdentity;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Determines whether the specified <see cref="System.Object" />, is equal to this instance.
        /// </summary>
        /// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
        /// <returns>
        ///   <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
        /// </returns>
        public override bool Equals(object obj)
        {
            bool result = false;
            OAuthTokenIdentifier identifier = obj as OAuthTokenIdentifier;

            if (identifier != null)
            {
                if (identifier.Audience.Equals(Audience, StringComparison.OrdinalIgnoreCase) &&
                    identifier.TenantDomain.Equals(TenantDomain, StringComparison.OrdinalIgnoreCase))
                {
                    result = true;
                }
            }

            return(result);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Get app token from AAD.
        /// </summary>
        /// <param name="audienceUri">The audience uri.</param>
        /// <returns>The Azure AAD App token.</returns>
        public async Task <string> GetTokenAsync(OAuthTokenIdentifier oauthIdentity)
        {
            var authenticationContext = new AuthenticationContext((new Uri(new Uri(aadAuthority), oauthIdentity.TenantDomain)).ToString(), false);

            // The ADAL will cache the token and auto refresh when it is expired, we don't need to call AcquireTokenByRefreshToken method and in ADAL 3, the AcquireTokenByRefreshToken method will be deleted.
            // The token cache use ConcurrentDictionary, it is thread safe.
            AuthenticationResult authenticateResult;

            if (string.IsNullOrEmpty(this.aadClientSecret))//using cert if secret is empty or null
            {
                var clientCred = new ClientAssertionCertificate(this.aadClientId, this.aadAppCert);
                authenticateResult = await authenticationContext.AcquireTokenAsync(oauthIdentity.Audience, clientCred).ConfigureAwait(false);
            }
            else
            {
                var clientCredSecret = new ClientCredential(this.aadClientId, this.aadClientSecret);
                authenticateResult = await authenticationContext.AcquireTokenAsync(oauthIdentity.Audience, clientCredSecret).ConfigureAwait(false);
            }
            return(authenticateResult.CreateAuthorizationHeader());
        }
 /// <summary>
 /// Gets the restful client.
 /// </summary>
 /// <param name="oauthIdentity">The oauth identity.</param>
 /// <param name="tokenProvider">The token provider.</param>
 /// <returns>IRestfulClient.</returns>
 public IRestfulClient GetRestfulClient(OAuthTokenIdentifier oauthIdentity, ITokenProvider tokenProvider)
 {
     return(m_oAuthEvoHttpClientCache.GetOrCreate(oauthIdentity,
                                                  () => new OauthEvoRestfulClient(tokenProvider, oauthIdentity)));
 }