From 7094c57353babe1c593d64b27278a5d60a8f5dff Mon Sep 17 00:00:00 2001 From: DEV Date: Sat, 7 Dec 2024 00:48:22 +0100 Subject: [PATCH] Firs Post --- brbac/active.go | 25 +++++++ brbac/go.mod | 3 + brbac/role_id.go | 27 ++++++++ brbac/tests/go.mod | 3 + brbac/tests/user_test.go | 111 +++++++++++++++++++++++++++++++ brbac/user.go | 58 ++++++++++++++++ brbac/user_group_countries.go | 121 ++++++++++++++++++++++++++++++++++ brbac/user_group_provider.go | 31 +++++++++ brbac/user_id.go | 27 ++++++++ brbac/user_state.go | 114 ++++++++++++++++++++++++++++++++ go.mod | 3 + go.work | 7 ++ 12 files changed, 530 insertions(+) create mode 100644 brbac/active.go create mode 100644 brbac/go.mod create mode 100644 brbac/role_id.go create mode 100644 brbac/tests/go.mod create mode 100644 brbac/tests/user_test.go create mode 100644 brbac/user.go create mode 100644 brbac/user_group_countries.go create mode 100644 brbac/user_group_provider.go create mode 100644 brbac/user_id.go create mode 100644 brbac/user_state.go create mode 100644 go.mod create mode 100644 go.work diff --git a/brbac/active.go b/brbac/active.go new file mode 100644 index 0000000..3beb0f4 --- /dev/null +++ b/brbac/active.go @@ -0,0 +1,25 @@ +// ------------------------------------------------------------------------ +// 馃殌 Project Active8 +// 馃敆 UserStateActive Thing (activething.com) git.activething.com/go +// +// File name active.go +// 鉁嶏笍 Created by DEV +// 馃搮 Modified 04/12/2024 +// +// 馃敀 Copyright 2024 activething.com +// +// 馃摐 LICENSE -------------------------------------------------------------- +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ------------------------------------------------------------------------ + +package brbac diff --git a/brbac/go.mod b/brbac/go.mod new file mode 100644 index 0000000..e52f5f2 --- /dev/null +++ b/brbac/go.mod @@ -0,0 +1,3 @@ +module brbac + +go 1.22 diff --git a/brbac/role_id.go b/brbac/role_id.go new file mode 100644 index 0000000..8596a18 --- /dev/null +++ b/brbac/role_id.go @@ -0,0 +1,27 @@ +// ------------------------------------------------------------------------ +// 馃殌 Project Active8 +// 馃敆 Active Thing (activething.com) git.activething.com/go +// +// File name role_id.go +// 鉁嶏笍 Created by DEV +// 馃搮 Modified 06/12/2024 +// +// 馃敀 Copyright 2024 activething.com +// +// 馃摐 LICENSE -------------------------------------------------------------- +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ------------------------------------------------------------------------ + +package brbac + +type RoleID uint64 diff --git a/brbac/tests/go.mod b/brbac/tests/go.mod new file mode 100644 index 0000000..b9b8073 --- /dev/null +++ b/brbac/tests/go.mod @@ -0,0 +1,3 @@ +module tests + +go 1.22 diff --git a/brbac/tests/user_test.go b/brbac/tests/user_test.go new file mode 100644 index 0000000..5d087f5 --- /dev/null +++ b/brbac/tests/user_test.go @@ -0,0 +1,111 @@ +// ------------------------------------------------------------------------ +// 馃殌 Project Active8 +// 馃敆 UserStateActive Thing (activething.com) git.activething.com/go +// +// File name user_test.go +// 鉁嶏笍 Created by DEV +// 馃搮 Modified 04/12/2024 +// +// 馃敀 Copyright 2024 activething.com +// +// 馃摐 LICENSE -------------------------------------------------------------- +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ------------------------------------------------------------------------ + +package tests + +import ( + "brbac" + "testing" +) + +func TestUserID(t *testing.T) { + // Test creating a new User + source := byte(42) + state := byte(7) + id := uint64(0x123456789ABC) + + uid := brbac.NewUserID(source, state, id) + + // Verify source + if got := uid.Source(); got != source { + t.Errorf("Group: expected %d, got %d", source, got) + } + + // Verify state + if got := uid.State(); got != state { + t.Errorf("State: expected %d, got %d", state, got) + } + + // Verify ID + if got := uid.ID(); got != id { + t.Errorf("ID: expected 0x%X, got 0x%X", id, got) + } +} + +func TestUserIDSetters(t *testing.T) { + var uid brbac.User + + // Test SetGroup + source := byte(55) + uid = uid.SetGroup(source) + if got := uid.Group(); got != source { + t.Errorf("SetGroup: expected %d, got %d", source, got) + } + + // Test SetState + state := byte(22) + uid = uid.SetState(state) + if got := uid.State(); got != state { + t.Errorf("SetState: expected %d, got %d", state, got) + } + + // Test SetID + id := uint64(0xFFFFFFFFFFFF) + uid = uid.SetID(id) + if got := uid.ID(); got != id { + t.Errorf("SetID: expected 0x%X, got 0x%X", id, got) + } +} + +func TestUserIDChaining(t *testing.T) { + source := byte(33) + state := byte(11) + id := uint64(0x123456789ABC) + + uid := brbac.NewUserID(source, state, id) + + // Verify all components after chained creation + if got := uid.Source(); got != source { + t.Errorf("Chained Group: expected %d, got %d", source, got) + } + + if got := uid.State(); got != state { + t.Errorf("Chained State: expected %d, got %d", state, got) + } + + if got := uid.ID(); got != id { + t.Errorf("Chained ID: expected 0x%X, got 0x%X", id, got) + } +} + +func TestUserIDOverflow(t *testing.T) { + // Test that ID is truncated to 48 bits + fullID := uint64(0xFFFFFFFFFFFFFFFF) + uid := brbac.User(0).SetID(fullID) + + expectedID := fullID & brbac.userIdMask + if got := uid.ID(); got != expectedID { + t.Errorf("ID Overflow: expected 0x%X, got 0x%X", expectedID, got) + } +} diff --git a/brbac/user.go b/brbac/user.go new file mode 100644 index 0000000..c0952e1 --- /dev/null +++ b/brbac/user.go @@ -0,0 +1,58 @@ +// ------------------------------------------------------------------------ +// 馃殌 Project Active8 +// 馃敆 UserStateActive Thing (activething.com) git.activething.com/go +// +// File name user.go +// 鉁嶏笍 Created by DEV +// 馃搮 Modified 04/12/2024 +// +// 馃敀 Copyright 2024 activething.com +// +// 馃摐 LICENSE -------------------------------------------------------------- +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ------------------------------------------------------------------------ + +package brbac + +const ( + userGpMask uint64 = 0xFF << 56 + userIdMask uint64 = 0x00FFFFFFFFFFFFFF +) + +type ( + User uint64 +) + +// NewUser creates a new User with optional components +func NewUser(groupID byte, id uint64) User { + var uid User + return uid.SetGroup(groupID) | uid.SetID(id) +} + +func (u User) ID() uint64 { return uint64(u) & userIdMask } + +func (u User) Group() byte { return byte((uint64(u) & userGpMask) >> 48) } + +// SetGroup updates the group bits of the User +func (u User) SetGroup(groupId byte) User { + return User((uint64(u) & ^userGpMask) | (uint64(groupId) << 48)) +} + +// SetID updates the 48-bit identifier of the User +func (u User) SetID(id uint64) User { + if id > userIdMask { + //todo + panic("Excced max id") + } + return User((uint64(u) & ^userIdMask) | (id & userIdMask)) +} diff --git a/brbac/user_group_countries.go b/brbac/user_group_countries.go new file mode 100644 index 0000000..1daf081 --- /dev/null +++ b/brbac/user_group_countries.go @@ -0,0 +1,121 @@ +// ------------------------------------------------------------------------ +// 馃殌 Project Active8 +// 馃敆 UserStateActive Thing (activething.com) git.activething.com/go +// +// File name user_group_countries.go +// 鉁嶏笍 Created by DEV +// 馃搮 Modified 04/12/2024 +// +// 馃敀 Copyright 2024 activething.com +// +// 馃摐 LICENSE -------------------------------------------------------------- +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ------------------------------------------------------------------------ + +package brbac + +var ( + codesCountries = [256]string{ + "", + "AD", "AE", "AF", "AG", "AI", "AL", "AM", "AO", "AQ", "AR", "AS", "AT", "AU", "AW", "AX", "AZ", + "BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BL", "BM", "BN", "BO", "BQ", "BR", "BS", + "BT", "BV", "BW", "BY", "BZ", + "CA", "CC", "CD", "CF", "CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO", "CR", "CU", "CV", "CW", + "CX", "CY", "CZ", + "DE", "DJ", "DK", "DM", "DO", "DZ", + "EC", "EE", "EG", "EH", "ER", "ES", "ET", + "FI", "FJ", "FK", "FM", "FO", "FR", + "GA", "GB", "GD", "GE", "GF", "GG", "GH", "GI", "GL", "GM", "GN", "GP", "GQ", "GR", "GS", "GT", + "GU", "GW", "GY", + "HK", "HM", "HN", "HR", "HT", "HU", + "ID", "IE", "IL", "IM", "IN", "IO", "IQ", "IR", "IS", "IT", + "JE", "JM", "JO", "JP", "KE", + "KG", "KH", "KI", "KM", "KN", "KP", "KR", "KW", "KY", "KZ", + "LA", "LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU", "LV", "LY", + "MA", "MC", "MD", "ME", "MF", "MG", "MH", "MK", "ML", "MM", "MN", "MO", "MP", "MQ", + "MR", "MS", "MT", "MU", "MV", "MW", "MX", "MY", "MZ", + "NA", "NC", "NE", "NF", "NG", "NI", "NL", "NO", "NP", "NR", "NU", "NZ", + "OM", + "PA", "PE", "PF", "PG", "PH", "PK", "PL", "PM", "PN", "PR", "PT", "PW", "PY", + "QA", + "RE", "RO", "RS", "RU", "RW", + "SA", "SB", "SC", "SD", "SE", "SG", "SH", "SI", "SJ", "SK", "SL", "SM", "SN", "SO", + "SR", "SS", "ST", "SV", "SX", "SY", "SZ", + "TC", "TD", "TF", "TG", "TH", "TJ", "TK", "TL", "TM", "TN", "TO", "TR", "TT", "TV", + "TW", "TZ", + "UA", "UG", "UM", "US", "UY", "UZ", + "VA", "VC", "VE", "VG", "VI", "VN", "VU", + "WF", "WS", + "YE", "YT", + "ZA", "ZM", "ZW", + } + + countriesCodes = fillCountriesCode() +) + +func fillCountriesCode() map[string]byte { + m := make(map[string]byte, len(codesCountries)) + for i := 0; i < len(codesCountries); i++ { + m[codesCountries[i]] = byte(i) + } + return m +} + +type ( + UserSourceCountries struct{} +) + +// GetAlpha Obtener el c贸digo alfab茅tico de un pa铆s usando el 铆ndice +func (_ UserSourceCountries) GetAlpha(code byte) (string, bool) { + // Verificar que el c贸digo est茅 dentro del rango v谩lido + c := int(code) + if c >= 1 && c < len(codesCountries) { + return codesCountries[c], true + } + // Si el c贸digo no es v谩lido, retornar un error + return "", false +} + +// GetCode Obtener el c贸digo num茅rico del pa铆s usando el c贸digo alfab茅tico +func (_ UserSourceCountries) GetCode(alpha string) byte { + // Verificar si el c贸digo alfab茅tico existe en el mapa + if code, exists := countriesCodes[alpha]; exists { + return code + } + // Si el c贸digo no existe, retornar 0 o alg煤n valor que indique error + return 0 +} + +// ProvideUserSource Proveer el c贸digo de usuario basado en un c贸digo de pa铆s (string o byte) +func (_ UserSourceCountries) ProvideUserSource(code any) byte { + switch v := code.(type) { + case string: + // Si el c贸digo es un string (alfab茅tico), obtenemos el c贸digo num茅rico + if c, exists := countriesCodes[v]; exists { + return c + } + //todo + panic("invalid country code") + case byte: + vl := int(v) + // Si el c贸digo es un byte, aseguramos que est茅 en el rango v谩lido + if vl >= 1 && vl < len(codesCountries) { + return v + } + //todo + panic("invalid country code") + default: + //todo + panic("invalid code type") + } +} diff --git a/brbac/user_group_provider.go b/brbac/user_group_provider.go new file mode 100644 index 0000000..0256562 --- /dev/null +++ b/brbac/user_group_provider.go @@ -0,0 +1,31 @@ +// ------------------------------------------------------------------------ +// 馃殌 Project Active8 +// 馃敆 UserStateActive Thing (activething.com) git.activething.com/go +// +// File name user_group_provider.go +// 鉁嶏笍 Created by DEV +// 馃搮 Modified 04/12/2024 +// +// 馃敀 Copyright 2024 activething.com +// +// 馃摐 LICENSE -------------------------------------------------------------- +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ------------------------------------------------------------------------ + +package brbac + +type ( + UserSourceProvider interface { + ProvideUserSource(code any) byte + } +) diff --git a/brbac/user_id.go b/brbac/user_id.go new file mode 100644 index 0000000..f4bd49d --- /dev/null +++ b/brbac/user_id.go @@ -0,0 +1,27 @@ +// ------------------------------------------------------------------------ +// 馃殌 Project Active8 +// 馃敆 Active Thing (activething.com) git.activething.com/go +// +// File name user_id.go +// 鉁嶏笍 Created by DEV +// 馃搮 Modified 06/12/2024 +// +// 馃敀 Copyright 2024 activething.com +// +// 馃摐 LICENSE -------------------------------------------------------------- +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ------------------------------------------------------------------------ + +package brbac + +type UserID uint64 diff --git a/brbac/user_state.go b/brbac/user_state.go new file mode 100644 index 0000000..20df5de --- /dev/null +++ b/brbac/user_state.go @@ -0,0 +1,114 @@ +// ------------------------------------------------------------------------ +// 馃殌 Project Active8 +// 馃敆 UserStateActive Thing (activething.com) git.activething.com/go +// +// File name user_state.go +// 鉁嶏笍 Created by DEV +// 馃搮 Modified 04/12/2024 +// +// 馃敀 Copyright 2024 activething.com +// +// 馃摐 LICENSE -------------------------------------------------------------- +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ------------------------------------------------------------------------ + +package brbac + +// Estados (4 bits para los estados) +const ( + UserStateActive UserState = 0b0000 // El usuario est谩 activo y no tiene restricciones + UserStateInactive UserState = 0b0001 // El usuario est谩 inactivo y temporalmente deshabilitado + UserStatePending UserState = 0b0010 // El usuario est谩 pendiente de activaci贸n (por ejemplo, verificaci贸n de correo electr贸nico) + UserStateSuspended UserState = 0b0011 // El usuario est谩 suspendido debido a problemas de seguridad o pol铆ticas + UserStateRevoked UserState = 0b0100 // El acceso del usuario ha sido revocado permanentemente + UserStateExpired UserState = 0b0101 // La cuenta del usuario ha expirado (por ejemplo, acceso temporal) + UserStatePaused UserState = 0b0110 // El usuario ha pausado su cuenta (puede reactivarse) + UserStateDeleted UserState = 0b0111 // La cuenta del usuario est谩 eliminada y ya no existe + +) + +// Flags (4 bits para los flags) +const ( + UserFlagRestricted UserState = 0b00010000 // El acceso del usuario est谩 limitado (restringido a ciertos recursos) + UserFlagSupervised UserState = 0b10000000 // El usuario esta supervisado +) + +type UserState byte + +func (s UserState) Set(newState UserState, flags ...UserState) UserState { + if len(flags) == 0 { + return (s & 0b11110000) | (newState & 0b1111) + } + return (s & 0b11110000) | (newState & 0b1111) | (s & 0b00001111) | (flags[0] & 0b11110000) +} + +// SetState Establecer el estado sin afectar los flags +func (s UserState) SetState(newState UserState) UserState { + // Usamos una m谩scara para preservar los flags y solo cambiar el estado + return (s & 0b11110000) | (newState & 0b1111) +} + +// SetFlags Establecer los flags sin afectar el estado +func (s UserState) SetFlags(newFlags UserState) UserState { + // Usamos una m谩scara para preservar el estado y solo cambiar los flags + return (s & 0b00001111) | (newFlags & 0b11110000) +} + +// IsActive verifica si el usuario est谩 activo, ignorando los flags +func (s UserState) IsActive() bool { + // Compara solo los primeros 4 bits (estado) con el valor de UserStateActive + return (s & 0b1111) == UserStateActive +} + +// IsInactive verifica si el usuario est谩 inactivo, ignorando los flags +func (s UserState) IsInactive() bool { + return (s & 0b1111) == UserStateInactive +} + +// IsPending verifica si el usuario est谩 pendiente de activaci贸n, ignorando los flags +func (s UserState) IsPending() bool { + return (s & 0b1111) == UserStatePending +} + +// IsSuspended verifica si el usuario est谩 suspendido, ignorando los flags +func (s UserState) IsSuspended() bool { + return (s & 0b1111) == UserStateSuspended +} + +// IsRevoked verifica si el acceso del usuario ha sido revocado, ignorando los flags +func (s UserState) IsRevoked() bool { + return (s & 0b1111) == UserStateRevoked +} + +// IsPaused verifica si el usuario ha pausado su cuenta, ignorando los flags +func (s UserState) IsPaused() bool { + return (s & 0b1111) == UserStatePaused +} + +// IsExpired verifica si la cuenta del usuario ha expirado, ignorando los flags +func (s UserState) IsExpired(state UserState) bool { + return (s & 0b1111) == UserStateExpired +} + +// IsDeleted verifica si la cuenta del usuario ha sido eliminada, ignorando los flags +func (s UserState) IsDeleted() bool { + return (s & 0b1111) == UserStateDeleted +} + +func (s UserState) HasSupervision() bool { + return s&UserFlagRestricted != 0 +} + +func (s UserState) HasRestriction() bool { + return s&UserFlagSupervised != 0 +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..6db59fb --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module Active8 + +go 1.22 diff --git a/go.work b/go.work new file mode 100644 index 0000000..3528a75 --- /dev/null +++ b/go.work @@ -0,0 +1,7 @@ +go 1.22 + +use ( + . + brbac + brbac/tests +) \ No newline at end of file