Example #1
0
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            var    aadTenant = request.DataStore.GetValue("AADTenant");
            string authBase  = string.Format(Constants.AzureAuthUri, aadTenant);

            string oauthType = (request.DataStore.GetValue("oauthType") ?? string.Empty).ToLowerInvariant();
            var    meta      = AzureTokenUtility.GetMetaFromOAuthType(oauthType);

            switch (oauthType)
            {
            case "keyvault":
                var registrationResponse = RegisterKeyVault(request);
                if (!registrationResponse.IsSuccess)
                {
                    return(registrationResponse);
                }
                break;
            }

            string authUri = AzureTokenUtility.GetAzureAuthUri(oauthType, request.Info.WebsiteRootUrl + Constants.WebsiteRedirectPath, authBase);

            return(new ActionResponse(ActionStatus.Success, JsonUtility.GetJObjectFromStringValue(authUri.ToString())));
        }
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            // Handle Azure token slightly diffrent - depends on client id
            if (request.DataStore.GetValue("MsCrmToken") == null && request.DataStore.GetValue("AzureToken") != null && request.DataStore.GetJson("AzureToken", "expires_on") != null)
            {
                var expiryDateTime = UnixTimeStampToDateTime(request.DataStore.GetJson("AzureToken", "expires_on"));
                if ((expiryDateTime - DateTime.Now).TotalMinutes < 5)
                {
                    var dataStoreItem = request.DataStore.GetDataStoreItem("AzureToken");
                    var meta          = AzureTokenUtility.GetMetaFromOAuthType("");
                    var newToken      = AzureTokenUtility.GetTokenForResourceFromExistingToken("", request.Info.WebsiteRootUrl, dataStoreItem.Value, meta.Resource);
                    UpdateToken(dataStoreItem.Value, newToken);
                }
            }

            // Handle Azure token slightly diffrent - depends on client id - use mscrm client id to refresh the token
            // Checks for both tokens in the CRMSalesManagement case
            if (request.DataStore.GetValue("MsCrmToken") != null && request.DataStore.GetValue("AzureToken") != null && request.DataStore.GetJson("AzureToken", "expires_on") != null)
            {
                var expiryDateTime = UnixTimeStampToDateTime(request.DataStore.GetJson("AzureToken", "expires_on"));
                if ((expiryDateTime - DateTime.Now).TotalMinutes < 5)
                {
                    var dataStoreItem = request.DataStore.GetDataStoreItem("AzureToken");
                    var meta          = AzureTokenUtility.GetMetaFromOAuthType("mscrm");
                    var newToken      = AzureTokenUtility.GetTokenForResourceFromExistingToken("mscrm", request.Info.WebsiteRootUrl, dataStoreItem.Value, Constants.AzureManagementCoreApi);
                    UpdateToken(dataStoreItem.Value, newToken);
                }
            }

            if (request.DataStore.GetValue("AzureTokenKV") != null && request.DataStore.GetJson("AzureTokenKV", "expires_on") != null)
            {
                var expiryDateTime = UnixTimeStampToDateTime(request.DataStore.GetJson("AzureTokenKV", "expires_on"));
                if ((expiryDateTime - DateTime.Now).TotalMinutes < 5)
                {
                    var dataStoreItem = request.DataStore.GetDataStoreItem("AzureTokenKV");
                    var meta          = AzureTokenUtility.GetMetaFromOAuthType("keyvault");
                    var newToken      = AzureTokenUtility.GetTokenForResourceFromExistingToken("keyvault", request.Info.WebsiteRootUrl, dataStoreItem.Value, meta.Resource);
                    UpdateToken(dataStoreItem.Value, newToken);
                }
            }

            // Checks for crmtoken expiry in CrmSalesManagement
            if (request.DataStore.GetValue("MsCrmToken") != null && request.DataStore.GetJson("MsCrmToken", "expires_on") != null)
            {
                var expiryDateTime = UnixTimeStampToDateTime(request.DataStore.GetJson("MsCrmToken", "expires_on"));
                if ((expiryDateTime - DateTime.Now).TotalMinutes < 5)
                {
                    var dataStoreItem = request.DataStore.GetDataStoreItem("MsCrmToken");
                    var meta          = AzureTokenUtility.GetMetaFromOAuthType("mscrm");
                    var newToken      = AzureTokenUtility.GetTokenForResourceFromExistingToken("mscrm", request.Info.WebsiteRootUrl, dataStoreItem.Value, meta.Resource);
                    UpdateToken(dataStoreItem.Value, newToken);
                }
            }

            if (request.DataStore.GetValue("AzureTokenAS") != null && request.DataStore.GetJson("AzureTokenAS", "expires_on") != null)
            {
                var expiryDateTime = UnixTimeStampToDateTime(request.DataStore.GetJson("AzureTokenAS", "expires_on"));
                if ((expiryDateTime - DateTime.Now).TotalMinutes < 5)
                {
                    var dataStoreItem = request.DataStore.GetDataStoreItem("AzureTokenAS");
                    var meta          = AzureTokenUtility.GetMetaFromOAuthType("as");
                    var newToken      = AzureTokenUtility.GetTokenForResourceFromExistingToken("as", request.Info.WebsiteRootUrl, dataStoreItem.Value, meta.Resource);
                    UpdateToken(dataStoreItem.Value, newToken);
                }
            }

            return(new ActionResponse(ActionStatus.Success));
        }
Example #3
0
        public static JObject GetTokenForResourceFromCode(string oauthType, string tenantId, string redirect, string code)
        {
            var meta = AzureTokenUtility.GetMetaFromOAuthType(oauthType);

            return(GetTokenForResourceFromCode(meta, tenantId, redirect, code));
        }