Beispiel #1
0
        public static bool RegisterUserAsPlaceholder(out string _UserID, IBDatabaseServiceInterface _DatabaseService, string _EmailWithoutPrefix, string _Name, List <string> _SSOSuperAdmins, Action <string> _ErrorMessageAction)
        {
            JArray NewBaseRights;

            if (_SSOSuperAdmins.Contains(_EmailWithoutPrefix))
            {
                if (!CreateUser(out _UserID, out _, _DatabaseService, _EmailWithoutPrefix, _Name, _ErrorMessageAction))
                {
                    return(false);
                }
                NewBaseRights = new JArray()
                {
                    JObject.Parse(JsonConvert.SerializeObject(
                                      new AccessScope()
                    {
                        WildcardPath = "*",
                        AccessRights = new List <string>()
                        {
                            "GET", "POST", "PUT", "DELETE"
                        }
                    }))
                };
            }
            else if (!CreateUser(out _UserID, out NewBaseRights, _DatabaseService, _EmailWithoutPrefix, _Name, _ErrorMessageAction))
            {
                return(false);
            }

            if (!Controller_Rights_Internal.Get().GrantUserWithRights(false, _UserID, NewBaseRights, _ErrorMessageAction))
            {
                return(false);
            }

            return(true);
        }
Beispiel #2
0
 public static Controller_Rights_Internal Get()
 {
     if (Instance == null)
     {
         Instance = new Controller_Rights_Internal();
     }
     return(Instance);
 }
Beispiel #3
0
        public bool RegisterAuthMethodAndMemoryEntryAfterRefresh(string _UserID, string _EmailAddressWithoutPostfix, int _ExpiresInSeconds, string _NewRefreshToken)
        {
            if (!MakeQueryParameters(out BMemoryQueryParameters QueryParameters, out string _PasswordMD5_FromAccessToken))
            {
                return(false);
            }

            JArray NewBaseRights;

            if (SSOSuperAdmins.Contains(_EmailAddressWithoutPostfix))
            {
                NewBaseRights = new JArray()
                {
                    JObject.Parse(JsonConvert.SerializeObject(
                                      new AccessScope()
                    {
                        WildcardPath = "*",
                        AccessRights = new List <string>()
                        {
                            "GET", "POST", "PUT", "DELETE"
                        }
                    }))
                };
            }
            else if (!TryGettingBaseRightsOrDefault(out NewBaseRights, _UserID, ErrorMessageAction))
            {
                return(false);
            }

            if (!CreateAuthMethod(out string _AccessMethod, _UserID, _EmailAddressWithoutPostfix, _PasswordMD5_FromAccessToken, ErrorMessageAction))
            {
                return(false);
            }

            if (!Controller_Rights_Internal.Get().GrantUserWithRights(false, _UserID, NewBaseRights, ErrorMessageAction))
            {
                return(false);
            }

            long ExpiresAt = new DateTimeOffset(DateTime.UtcNow.AddSeconds(_ExpiresInSeconds)).ToUnixTimeSeconds();

            MemoryService.SetKeyValue(
                QueryParameters,
                new Tuple <string, BPrimitiveType>[]
            {
                new Tuple <string, BPrimitiveType>(UserDBEntry.KEY_NAME_USER_ID, new BPrimitiveType(_UserID)),
                new Tuple <string, BPrimitiveType>("refresh_token", new BPrimitiveType(_NewRefreshToken)),
                new Tuple <string, BPrimitiveType>("expires_at", new BPrimitiveType(ExpiresAt))
            },
                ErrorMessageAction);
            MemoryService.SetKeyExpireTime(QueryParameters, TimeSpan.FromDays(7), ErrorMessageAction);

            return(true);
        }
Beispiel #4
0
        public static bool TryGettingBaseRightsOrDefault(out JArray _BaseRightsOrDefault, string _UserID, Action <string> _ErrorMessageAction)
        {
            if (!Controller_Rights_Internal.Get().GetUserDefaultRights(out _BaseRightsOrDefault, _UserID, _ErrorMessageAction))
            {
                return(false);
            }

            if (!GetUserBaseRights(_UserID, out JArray _ExistingBaseRights, _ErrorMessageAction))
            {
                return(false);
            }

            AccessScopeLibrary.UnionMergeRights(_BaseRightsOrDefault, _ExistingBaseRights);
            return(true);
        }
Beispiel #5
0
        private static bool CreateUser(out string _UserID, out JArray _BaseRightsOrDefault, IBDatabaseServiceInterface _DatabaseService, string _EmailWithoutPostfix, string _OptionalName, Action <string> _ErrorMessageAction)
        {
            _UserID = null;
            _BaseRightsOrDefault = null;

            var EmailAddressWithPostfix = _EmailWithoutPostfix + EMAIL_USER_NAME_POSTFIX;

            var Endpoint = "http://localhost:" + LocalServerPort + "/auth/users";

            using var Handler = new HttpClientHandler
                  {
                      SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls,
                      ServerCertificateCustomValidationCallback = (a, b, c, d) => true
                  };
            using var Client = new HttpClient(Handler);
            Client.DefaultRequestHeaders.TryAddWithoutValidation("internal-call-secret", CommonData.INTERNAL_CALL_PRIVATE_KEY);
            Client.DefaultRequestHeaders.TryAddWithoutValidation("do-not-get-db-clearance", "false");
            try
            {
                using var RequestContent = new StringContent(new JObject()
                {
                    [UserDBEntry.USER_EMAIL_PROPERTY] = EmailAddressWithPostfix,
                    [UserDBEntry.USER_NAME_PROPERTY]  = _OptionalName != null ? (_OptionalName + EMAIL_USER_NAME_POSTFIX) : EmailAddressWithPostfix
                }.ToString(), Encoding.UTF8, "application/json");

                using var RequestTask = Client.PutAsync(Endpoint, RequestContent);
                RequestTask.Wait();

                using var Response        = RequestTask.Result;
                using var ResponseContent = Response.Content;

                using var ReadResponseTask = ResponseContent.ReadAsStringAsync();
                ReadResponseTask.Wait();

                var ResponseString = ReadResponseTask.Result;

                if (!Response.IsSuccessStatusCode)
                {
                    if ((int)Response.StatusCode == BWebResponse.Error_Conflict_Code)
                    {
                        if (!_DatabaseService.GetItem(
                                UniqueUserFieldsDBEntry.DBSERVICE_UNIQUEUSERFIELDS_TABLE(),
                                UniqueUserFieldsDBEntry.KEY_NAME_USER_EMAIL,
                                new BPrimitiveType(EmailAddressWithPostfix),
                                UniqueUserFieldsDBEntry.Properties,
                                out JObject _ExistenceCheck,
                                _ErrorMessageAction))
                        {
                            return(false);
                        }

                        _UserID = (string)_ExistenceCheck[UserDBEntry.KEY_NAME_USER_ID];

                        if (!TryGettingBaseRightsOrDefault(out _BaseRightsOrDefault, _UserID, _ErrorMessageAction))
                        {
                            _ErrorMessageAction?.Invoke("Error: Controller_SSOAccessToken->CreateUser: TryGettingBaseRightsOrDefault has failed.");
                            return(false);
                        }

                        return(true);
                    }
                    _ErrorMessageAction?.Invoke("Error: Controller_SSOAccessToken->CreateUser: Request returned error. Endpoint: " + Endpoint + ", code: " + Response.StatusCode + ", message: " + ResponseString);
                    return(false);
                }

                _UserID = (string)JObject.Parse(ResponseString)[UserDBEntry.KEY_NAME_USER_ID];
                if (!Controller_Rights_Internal.Get().GetUserDefaultRights(out _BaseRightsOrDefault, _UserID, _ErrorMessageAction))
                {
                    _ErrorMessageAction?.Invoke("Error: Controller_SSOAccessToken->CreateUser: GetUserDefaultRights has failed.");
                    return(false);
                }
            }
            catch (Exception e)
            {
                if (e.InnerException != null && e.InnerException != e)
                {
                    _ErrorMessageAction?.Invoke("Error: Controller_SSOAccessToken->CreateUser->Inner: " + e.InnerException.Message + ", Trace: " + e.InnerException.StackTrace);
                }
                if (e is AggregateException)
                {
                    foreach (var Inner in (e as AggregateException).InnerExceptions)
                    {
                        _ErrorMessageAction?.Invoke("Error: Controller_SSOAccessToken->CreateUser->Aggregate->Inner: " + Inner.Message + ", Trace: " + Inner.StackTrace);
                    }
                }
                _ErrorMessageAction?.Invoke("Error: Controller_SSOAccessToken->CreateUser: Request failed. Endpoint: " + Endpoint + ", message: " + e.Message + ", trace: " + e.StackTrace);
                return(false);
            }
            return(true);
        }