예제 #1
0
        private static Token GetToken(string code)
        {
            var data = string.Format("code={0}&client_id={1}&client_secret={2}&grant_type=authorization_code",
                                     HttpUtility.UrlEncode(code),
                                     HttpUtility.UrlEncode(ClientId),
                                     HttpUtility.UrlEncode(SecretKey));

            var jsonToken = RequestHelper.PerformRequest(BoxUrlToken, "application/x-www-form-urlencoded", "POST", data);

            Global.Logger.Debug("BoxApp: token response - " + jsonToken);

            return(Token.FromJson(jsonToken));
        }
예제 #2
0
        private static string GetDriveFile(string googleFileId, Token token)
        {
            if (token == null)
            {
                Global.Logger.Error("GoogleDriveApp: token is null");
                throw new SecurityException("Access token is null");
            }

            var resultResponse = RequestHelper.PerformRequest(GoogleUrlFile.Replace("{fileId}", googleFileId), headers: new Dictionary <string, string> {
                { "Authorization", "Bearer " + token.AccessToken }
            });

            Global.Logger.Debug("GoogleDriveApp: file response - " + resultResponse);
            return(resultResponse);
        }
예제 #3
0
        public Token RefreshToken(string refreshToken)
        {
            Global.Logger.Debug("BoxApp: refresh token");

            var query = String.Format("client_id={0}&client_secret={1}&refresh_token={2}&grant_type=refresh_token",
                                      HttpUtility.UrlEncode(ClientId),
                                      HttpUtility.UrlEncode(SecretKey),
                                      HttpUtility.UrlEncode(refreshToken));

            var jsonToken = RequestHelper.PerformRequest(BoxUrlToken, "application/x-www-form-urlencoded", "POST", query);

            Global.Logger.Debug("BoxApp: refresh token response - " + jsonToken);
            var token = Token.FromJson(jsonToken);

            if (token != null)
            {
                Token.SaveToken(AppAttr, token);
            }
            return(token);
        }
예제 #4
0
        private static Token GetToken(string code)
        {
            var data = string.Format("code={0}&client_id={1}&client_secret={2}&redirect_uri={3}&grant_type=authorization_code",
                                     HttpUtility.UrlEncode(code),
                                     HttpUtility.UrlEncode(ClientId),
                                     HttpUtility.UrlEncode(SecretKey),
                                     RedirectUrl);

            try
            {
                var jsonToken = RequestHelper.PerformRequest(GoogleUrlToken, "application/x-www-form-urlencoded", "POST", data);
                Global.Logger.Debug("GoogleDriveApp: token response - " + jsonToken);

                return(Token.FromJson(AppAttr, jsonToken));
            }
            catch (Exception ex)
            {
                Global.Logger.Error(ex);
            }
            return(null);
        }
예제 #5
0
 private static string GetDriveFile(string googleFileId, Token token)
 {
     if (token == null)
     {
         Global.Logger.Error("GoogleDriveApp: token is null");
         throw new SecurityException("Access token is null");
     }
     try
     {
         var resultResponse = RequestHelper.PerformRequest(GoogleLoginProvider.GoogleUrlFile + googleFileId,
                                                           headers: new Dictionary <string, string> {
             { "Authorization", "Bearer " + token }
         });
         Global.Logger.Debug("GoogleDriveApp: file response - " + resultResponse);
         return(resultResponse);
     }
     catch (Exception ex)
     {
         Global.Logger.Error("GoogleDriveApp: file request", ex);
     }
     return(null);
 }
예제 #6
0
        private string GetBoxFile(string boxFileId, Token token)
        {
            if (token == null)
            {
                Logger.Error("BoxApp: token is null");
                throw new SecurityException("Access token is null");
            }

            try
            {
                var resultResponse = RequestHelper.PerformRequest(BoxUrlFile.Replace("{fileId}", boxFileId),
                                                                  headers: new Dictionary <string, string> {
                    { "Authorization", "Bearer " + token }
                });
                Logger.Debug("BoxApp: file response - " + resultResponse);
                return(resultResponse);
            }
            catch (Exception ex)
            {
                Logger.Error("BoxApp: file request", ex);
            }
            return(null);
        }
예제 #7
0
        private UserInfo GetUserInfo(Token token, out bool isNew)
        {
            isNew = false;
            if (token == null)
            {
                Logger.Error("BoxApp: token is null");
                throw new SecurityException("Access token is null");
            }

            var resultResponse = string.Empty;

            try
            {
                resultResponse = RequestHelper.PerformRequest(BoxUrlUserInfo,
                                                              headers: new Dictionary <string, string> {
                    { "Authorization", "Bearer " + token }
                });
                Logger.Debug("BoxApp: userinfo response - " + resultResponse);
            }
            catch (Exception ex)
            {
                Logger.Error("BoxApp: userinfo request", ex);
            }

            var boxUserInfo = JObject.Parse(resultResponse);

            if (boxUserInfo == null)
            {
                Logger.Error("Error in userinfo request");
                return(null);
            }

            var email    = boxUserInfo.Value <string>("login");
            var userInfo = UserManager.GetUserByEmail(email);

            if (Equals(userInfo, Constants.LostUser))
            {
                userInfo = new UserInfo
                {
                    FirstName   = boxUserInfo.Value <string>("name"),
                    Email       = email,
                    MobilePhone = boxUserInfo.Value <string>("phone"),
                };

                var cultureName = boxUserInfo.Value <string>("language");
                if (string.IsNullOrEmpty(cultureName))
                {
                    cultureName = Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName;
                }
                var cultureInfo = SetupInfo.EnabledCultures.Find(c => string.Equals(c.TwoLetterISOLanguageName, cultureName, StringComparison.InvariantCultureIgnoreCase));
                if (cultureInfo != null)
                {
                    userInfo.CultureName = cultureInfo.Name;
                }
                else
                {
                    Logger.DebugFormat("From box app new personal user '{0}' without culture {1}", userInfo.Email, cultureName);
                }

                if (string.IsNullOrEmpty(userInfo.FirstName))
                {
                    userInfo.FirstName = FilesCommonResource.UnknownFirstName;
                }
                if (string.IsNullOrEmpty(userInfo.LastName))
                {
                    userInfo.LastName = FilesCommonResource.UnknownLastName;
                }

                try
                {
                    SecurityContext.AuthenticateMeWithoutCookie(ASC.Core.Configuration.Constants.CoreSystem);
                    userInfo = UserManagerWrapper.AddUser(userInfo, UserManagerWrapper.GeneratePassword());
                }
                finally
                {
                    AuthContext.Logout();
                }

                isNew = true;

                Logger.Debug("BoxApp: new user " + userInfo.ID);
            }

            return(userInfo);
        }
예제 #8
0
        private static UserInfo GetUserInfo(Token token, out bool isNew)
        {
            isNew = false;
            if (token == null)
            {
                Global.Logger.Error("GoogleDriveApp: token is null");
                throw new SecurityException("Access token is null");
            }

            var resultResponse = RequestHelper.PerformRequest(GoogleUrlUserInfo.Replace("{access_token}", token.AccessToken));

            Global.Logger.Debug("GoogleDriveApp: userinfo response - " + resultResponse);

            var googleUserInfo = JObject.Parse(resultResponse);

            if (googleUserInfo == null)
            {
                Global.Logger.Error("Error in userinfo request");
                return(null);
            }

            var email    = googleUserInfo.Value <string>("email");
            var userInfo = CoreContext.UserManager.GetUserByEmail(email);

            if (Equals(userInfo, Constants.LostUser))
            {
                userInfo = new UserInfo
                {
                    Status       = EmployeeStatus.Active,
                    FirstName    = googleUserInfo.Value <string>("given_name"),
                    LastName     = googleUserInfo.Value <string>("family_name"),
                    Email        = email,
                    WorkFromDate = TenantUtil.DateTimeNow(),
                };

                var gender = googleUserInfo.Value <string>("gender");
                if (!string.IsNullOrEmpty(gender))
                {
                    userInfo.Sex = gender == "male";
                }

                var cultureName = googleUserInfo.Value <string>("locale") ?? CultureInfo.CurrentUICulture.Name;
                var cultureInfo = SetupInfo.EnabledCultures.Find(c => String.Equals(c.TwoLetterISOLanguageName, cultureName, StringComparison.InvariantCultureIgnoreCase));
                if (cultureInfo != null)
                {
                    userInfo.CultureName = cultureInfo.Name;
                }

                if (string.IsNullOrEmpty(userInfo.FirstName))
                {
                    userInfo.FirstName = FilesCommonResource.UnknownFirstName;
                }
                if (string.IsNullOrEmpty(userInfo.LastName))
                {
                    userInfo.LastName = FilesCommonResource.UnknownLastName;
                }

                var pwd = UserManagerWrapper.GeneratePassword();

                try
                {
                    SecurityContext.AuthenticateMe(ASC.Core.Configuration.Constants.CoreSystem);
                    userInfo = UserManagerWrapper.AddUser(userInfo, pwd);
                }
                finally
                {
                    SecurityContext.Logout();
                }

                isNew = true;

                Global.Logger.Debug("GoogleDriveApp: new user " + userInfo.ID);
            }

            return(userInfo);
        }
예제 #9
0
        private static UserInfo GetUserInfo(Token token, out bool isNew)
        {
            isNew = false;
            if (token == null)
            {
                Global.Logger.Error("BoxApp: token is null");
                throw new SecurityException("Access token is null");
            }

            var resultResponse = RequestHelper.PerformRequest(BoxUrlUserInfo,
                                                              headers: new Dictionary <string, string> {
                { "Authorization", "Bearer " + token.AccessToken }
            });

            Global.Logger.Debug("BoxApp: userinfo response - " + resultResponse);

            var boxUserInfo = JObject.Parse(resultResponse);

            if (boxUserInfo == null)
            {
                Global.Logger.Error("Error in userinfo request");
                return(null);
            }

            var email    = boxUserInfo.Value <string>("login");
            var userInfo = CoreContext.UserManager.GetUserByEmail(email);

            if (Equals(userInfo, Constants.LostUser))
            {
                userInfo = new UserInfo
                {
                    Status       = EmployeeStatus.Active,
                    FirstName    = boxUserInfo.Value <string>("name"),
                    Email        = email,
                    MobilePhone  = boxUserInfo.Value <string>("phone"),
                    WorkFromDate = TenantUtil.DateTimeNow(),
                };

                var cultureName = boxUserInfo.Value <string>("language") ?? CultureInfo.CurrentUICulture.Name;
                var cultureInfo = SetupInfo.EnabledCultures.Find(c => String.Equals(c.TwoLetterISOLanguageName, cultureName, StringComparison.InvariantCultureIgnoreCase));
                if (cultureInfo != null)
                {
                    userInfo.CultureName = cultureInfo.Name;
                }

                if (string.IsNullOrEmpty(userInfo.FirstName))
                {
                    userInfo.FirstName = FilesCommonResource.UnknownFirstName;
                }
                if (string.IsNullOrEmpty(userInfo.LastName))
                {
                    userInfo.LastName = FilesCommonResource.UnknownLastName;
                }

                var pwd = UserManagerWrapper.GeneratePassword();

                try
                {
                    SecurityContext.AuthenticateMe(ASC.Core.Configuration.Constants.CoreSystem);
                    userInfo = UserManagerWrapper.AddUser(userInfo, pwd);
                }
                finally
                {
                    SecurityContext.Logout();
                }

                isNew = true;

                Global.Logger.Debug("BoxApp: new user " + userInfo.ID);
            }

            return(userInfo);
        }
예제 #10
0
        private static UserInfo GetUserInfo(Token token, out bool isNew)
        {
            isNew = false;
            if (token == null)
            {
                Global.Logger.Error("GoogleDriveApp: token is null");
                throw new SecurityException("Access token is null");
            }

            var resultResponse = string.Empty;

            try
            {
                resultResponse = RequestHelper.PerformRequest(GoogleUrlUserInfo.Replace("{access_token}", HttpUtility.UrlEncode(token.ToString())));
                Global.Logger.Debug("GoogleDriveApp: userinfo response - " + resultResponse);
            }
            catch (Exception ex)
            {
                Global.Logger.Error("GoogleDriveApp: userinfo request", ex);
            }

            var googleUserInfo = JObject.Parse(resultResponse);

            if (googleUserInfo == null)
            {
                Global.Logger.Error("Error in userinfo request");
                return(null);
            }

            var email    = googleUserInfo.Value <string>("email");
            var userInfo = CoreContext.UserManager.GetUserByEmail(email);

            if (Equals(userInfo, Constants.LostUser))
            {
                userInfo = new UserInfo
                {
                    FirstName = googleUserInfo.Value <string>("given_name"),
                    LastName  = googleUserInfo.Value <string>("family_name"),
                    Email     = email,
                };

                var gender = googleUserInfo.Value <string>("gender");
                if (!string.IsNullOrEmpty(gender))
                {
                    userInfo.Sex = gender == "male";
                }

                var cultureName = googleUserInfo.Value <string>("locale");
                if (string.IsNullOrEmpty(cultureName))
                {
                    cultureName = Thread.CurrentThread.CurrentUICulture.Name;
                }

                var cultureInfo = SetupInfo.EnabledCultures.Find(c => String.Equals(c.Name, cultureName, StringComparison.InvariantCultureIgnoreCase));
                if (cultureInfo != null)
                {
                    userInfo.CultureName = cultureInfo.Name;
                }
                else
                {
                    Global.Logger.DebugFormat("From google app new personal user '{0}' without culture {1}", userInfo.Email, cultureName);
                }

                if (string.IsNullOrEmpty(userInfo.FirstName))
                {
                    userInfo.FirstName = FilesCommonResource.UnknownFirstName;
                }
                if (string.IsNullOrEmpty(userInfo.LastName))
                {
                    userInfo.LastName = FilesCommonResource.UnknownLastName;
                }

                var pwd = UserManagerWrapper.GeneratePassword();

                try
                {
                    SecurityContext.AuthenticateMe(ASC.Core.Configuration.Constants.CoreSystem);
                    userInfo = UserManagerWrapper.AddUser(userInfo, pwd);
                }
                finally
                {
                    SecurityContext.Logout();
                }

                isNew = true;

                Global.Logger.Debug("GoogleDriveApp: new user " + userInfo.ID);
            }

            return(userInfo);
        }