Skip to content

Commit

Permalink
setup jwt cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
dezren39 committed Feb 21, 2024
1 parent 95f7a4c commit db24dc3
Show file tree
Hide file tree
Showing 20 changed files with 1,921 additions and 1,191 deletions.
65 changes: 61 additions & 4 deletions sources/identity/auth/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,35 @@ func GetConnectionMap(ctx context.Context) (*SafeConnectionMap, bool) {

type SafeConnectionMap struct {
mu sync.RWMutex
data map[string]Connection
data map[string]*Connection
}

// NewSafeConnectionMap creates and returns a new SafeConnectionMap
func NewSafeConnectionMap() *SafeConnectionMap {
return &SafeConnectionMap{
data: make(map[string]Connection),
data: make(map[string]*Connection),
}
}

// Get safely retrieves an element from the map
// Get safely retrieves a copy of an element from the map
func (sm *SafeConnectionMap) Get(key string) (Connection, bool) {
sm.mu.RLock()
defer sm.mu.RUnlock()
val, ok := sm.data[key]
return *val, ok
}

// GetRef safely retrieves a reference to an element from the map
// Be careful with this, as it allows you to modify the map
func (sm *SafeConnectionMap) GetRef(key string) (*Connection, bool) {
sm.mu.RLock()
defer sm.mu.RUnlock()
val, ok := sm.data[key]
return val, ok
}

// Set safely adds an element to the map
func (sm *SafeConnectionMap) Set(key string, value Connection) {
func (sm *SafeConnectionMap) Set(key string, value *Connection) {
sm.mu.Lock()
defer sm.mu.Unlock()
sm.data[key] = value
Expand All @@ -66,6 +75,20 @@ func (sm *SafeConnectionMap) Delete(key string) {

// All safely retrieves all elements from the map
func (sm *SafeConnectionMap) All() map[string]Connection {
sm.mu.RLock()
defer sm.mu.RUnlock()
data := make(map[string]Connection, len(sm.data))
for k, v := range sm.data {
data[k] = *v
}
return data
}

// All safely retrieves all elements from the map
// Be careful with this, as it allows you to modify the map
// Specifically, it returns a copy of the map, so modifying the map will not modify the original
// But the elements are references, so modifying the elements will modify the original.
func (sm *SafeConnectionMap) AllRef() map[string]*Connection {
sm.mu.RLock()
defer sm.mu.RUnlock()
return sm.data
Expand Down Expand Up @@ -94,6 +117,18 @@ func (sm *SafeConnectionMap) Values() []Connection {
sm.mu.RLock()
defer sm.mu.RUnlock()
values := make([]Connection, 0, len(sm.data))
for _, v := range sm.data {
values = append(values, *v)
}
return values
}

// ValuesRef safely retrieves all values from the map
// Be careful with this, as it allows you to modify the map
func (sm *SafeConnectionMap) ValuesRef() []*Connection {
sm.mu.RLock()
defer sm.mu.RUnlock()
values := make([]*Connection, 0, len(sm.data))
for _, v := range sm.data {
values = append(values, v)
}
Expand Down Expand Up @@ -489,6 +524,14 @@ func (b *Connection) JSON() (string, error) {
return string(jsonB), nil
}

func (s *SafeConnectionMap) JSON() (string, error) {
jsonB, err := json.Marshal(s.All())
if err != nil {
return "", fmt.Errorf("failed to marshal connection map: %v", err)
}
return string(jsonB), nil
}

func (b *Connection) HTML() (string, error) {
jsonString, err := b.JSON()
if err != nil {
Expand All @@ -497,6 +540,20 @@ func (b *Connection) HTML() (string, error) {
return "<pre style=\"white-space: pre-wrap; overflow-wrap: anywhere;\">" + jsonString + "</pre>", nil
}

func (s *SafeConnectionMap) Insert(connection *Connection) (*string, error) {
connectionID, err := connection.Insert()
if err != nil {
return nil, fmt.Errorf("failed to insert connection: %w", err)
}
s.Set(*connectionID, connection)

if connection.CookieID != nil {
cookieID := *connection.CookieID
s.Set(cookieID, connection)
}
return connectionID, nil
}

func (b *Connection) Insert() (*string, error) {
if b.ConnectionID != nil {
return nil, fmt.Errorf("inserting connection with existing connection id")
Expand Down
135 changes: 84 additions & 51 deletions sources/identity/build-libsql.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
#!/usr/bin/env pwsh
param(
[switch]$ForceInstallTempl,
[switch]$Update
[switch]$Update,
[switch]$SkipBuildWebJs,
[switch]$SkipBuildTempl,
[switch]$SkipBuildGoGenerate,
[switch]$SkipBuildGoModTidy,
[switch]$SkipBuildGoGet,
[switch]$SkipBuildGoBuild,
[switch]$SkipBuildGoExperiment
)

Set-StrictMode -Version Latest
Expand All @@ -17,6 +24,9 @@ if ($PSNativeCommandUseErrorActionPreference) {
$originalVerbosePreference = $VerbosePreference
$VerbosePreference = 'Continue'

Write-Verbose "script: $($MyInvocation.MyCommand.Name)"
Write-Verbose "psscriptroot: $PSScriptRoot"
Write-Verbose "full script path: $PSScriptRoot$([IO.Path]::DirectorySeparatorChar)$($MyInvocation.MyCommand.Name)"
Write-Verbose "originalVerbosePreference: $originalVerbosePreference"
Write-Verbose "VerbosePreference: $VerbosePreference"

Expand All @@ -28,76 +38,99 @@ try {
Write-Verbose "Current directory: $cwd"

try {

Write-Verbose "Set-Location $PSScriptRoot/web"

Set-Location $PSScriptRoot/web

if ($Update) {
Write-Verbose "npm install -g npm"
npm install -g npm
Write-Verbose "npm install -g npm-check-updates"
npm install -g npm-check-updates
Write-Verbose "ncu -g"
ncu -g
Write-Verbose "ncu -u"
ncu -u
Write-Verbose "sleep 1"
Start-Sleep 1
Write-Verbose "npm install"
npm install
} else {
Write-Verbose "npm ci"
npm ci
if (-not $SkipBuildWebJs) {

Write-Verbose "Set-Location $PSScriptRoot/web"

Set-Location $PSScriptRoot/web

if ($Update) {
Write-Verbose "npm install -g npm"
npm install -g npm
Write-Verbose "npm install -g npm-check-updates"
npm install -g npm-check-updates
Write-Verbose "ncu -g"
ncu -g
Write-Verbose "ncu -u"
ncu -u
Write-Verbose "sleep 1"
Start-Sleep 1
Write-Verbose "npm install"
npm install
} else {
Write-Verbose "npm ci"
npm ci
}

Write-Verbose "npm run build"
npm run build
}

Write-Verbose "npm run build"
npm run build

Write-Verbose "Set-Location $PSScriptRoot"
Set-Location $PSScriptRoot

if ([string]::IsNullOrEmpty($env:GOEXPERIMENT)) {
$env:GOEXPERIMENT = 'rangefunc'
Write-Verbose "Setting GOEXPERIMENT to $env:GOEXPERIMENT"
if (-not $SkipBuildGoExperiment) {
if ([string]::IsNullOrEmpty($env:GOEXPERIMENT)) {
$env:GOEXPERIMENT = 'rangefunc'
Write-Verbose "Setting GOEXPERIMENT to $env:GOEXPERIMENT"
}
Write-Verbose "GOEXPERIMENT: $env:GOEXPERIMENT"
}
Write-Verbose "GOEXPERIMENT: $env:GOEXPERIMENT"

if ($Update) {
if ($Update -and -not $SkipBuildGoGet) {
Write-Verbose "go get -u"
go get -u
} else {
if ($SkipBuildGoGet) {
Write-Verbose "Skipping go get"
}
}

if (-not $SkipBuildGoModTidy) {
Write-Verbose "go mod tidy"
go mod tidy
} else {
Write-Verbose "Skipping go mod tidy"
}

Write-Verbose "go mod tidy"
go mod tidy
if (-not $SkipBuildTempl) {

Install-Templ -Force:$ForceInstallTempl
Install-Templ -Force:$ForceInstallTempl

Write-Verbose "templ fmt"
templ fmt .
Write-Verbose "templ fmt"
templ fmt .

Write-Verbose "templ generate"
$generateOutput = templ generate
Write-Verbose "templ generate output:"
$generateOutput -split "`n" | ForEach-Object { Write-Verbose ($_ -replace "\(Γ£ù\)", "" -replace "\(Γ£ô\)", "") }
Write-Verbose "templ generate"
$generateOutput = templ generate
Write-Verbose "templ generate output:"
$generateOutput -split "`n" | ForEach-Object { Write-Verbose ($_ -replace "\(Γ£ù\)", "" -replace "\(Γ£ô\)", "") }

if ($generateOutput -match '' -or $generateOutput -match 'Γ£ù') {
Write-Verbose "templ generate failed"
throw "templ generate failed"
}
else {
Write-Verbose "templ generate succeeded"
if ($generateOutput -match '' -or $generateOutput -match 'Γ£ù') {
Write-Verbose "templ generate failed"
throw "templ generate failed"
}
else {
Write-Verbose "templ generate succeeded"
}
} else {
Write-Verbose "Skipping templ build"
}

Write-Verbose "go generate ./..."
go generate ./...
if (-not $SkipBuildGoGenerate) {
Write-Verbose "go generate ./..."
go generate ./...
}

$tags = [System.IO.Path]::GetFileNameWithoutExtension($MyInvocation.MyCommand.Name) -replace '(?i)^build-', '' -replace '-', ',' -replace ' ', ','
if (-not $SkipBuildGoBuild) {
$tags = [System.IO.Path]::GetFileNameWithoutExtension($MyInvocation.MyCommand.Name) -replace '(?i)^build-', '' -replace '-', ',' -replace ' ', ','

Write-Verbose "tags: $tags"
Write-Verbose "tags: $tags"

Write-Verbose "go build -v -tags $tags"
go build -v -tags $tags
Write-Verbose "go build -v -tags $tags"
go build -v -tags $tags
} else {
Write-Verbose "Skipping go build"
}
}
finally {
Write-Verbose "Set-Location $cwd"
Expand Down
Loading

0 comments on commit db24dc3

Please sign in to comment.