|
| 1 | +Param( |
| 2 | + [Parameter(Mandatory=$False)] |
| 3 | + [string]$RepoUrl="localhost:51016", |
| 4 | + [Parameter(Mandatory=$False)] |
| 5 | + [string]$AuthUrl="localhost:51017", |
| 6 | + [Parameter(Mandatory=$False)] |
| 7 | + [string]$DataSource="sensenet-insql-cdb-sndb", |
| 8 | + [Parameter(Mandatory=$False)] |
| 9 | + [string]$DataContainer="sensenet-insql-cdb-snsql", |
| 10 | + [Parameter(Mandatory=$False)] |
| 11 | + [switch]$StepByStep |
| 12 | +) |
| 13 | + |
| 14 | +if (-not (Get-Command "Invoke-Cli" -DryRun $DryRun -ErrorAction SilentlyContinue)) { |
| 15 | + Write-Output "load helper functions" |
| 16 | + . "$($PSScriptRoot)/helper-functions.ps1" |
| 17 | +} |
| 18 | + |
| 19 | +Test-Docker |
| 20 | + |
| 21 | +$user="sa" |
| 22 | +$password="SuP3rS3CuR3P4sSw0Rd" |
| 23 | + |
| 24 | +$clientId = "pr3Gen3R4Ted" |
| 25 | +$clientSecret = "pr3Gen3R4Tedpr3Gen3R4Tedpr3Gen3R4Tedpr3Gen3R4Tedpr3Gen3R4Ted" |
| 26 | +$apiKey = "pr3Gen3R4Tedpr3Gen3R4Tedpr3Gen3R4Tedpr3Gen3R4Tedpr3Gen3R4Tedpr3Gen3R4Tedpr3Gen3R4Ted" |
| 27 | + |
| 28 | +# check if client exists |
| 29 | +$query = "SELECT * FROM [AccessTokens] WHERE [Value] = '$apiKey' AND [UserId] = 1 FOR JSON AUTO, WITHOUT_ARRAY_WRAPPER" |
| 30 | +$params = "exec", $DataContainer, "/opt/mssql-tools/bin/sqlcmd", "-d", $DataSource, "-U", $user, "-P", $password, "-Q", $query |
| 31 | +$result = Invoke-Cli -execFile "docker" -params $params -ErrorAction SilentlyContinue |
| 32 | +if ($result -ne "" -and $result -ne $null -and $result.Count -gt 1 -and $result[2] -ne $null) { |
| 33 | + $obj = $result[2] | ConvertFrom-Json |
| 34 | + if ($obj -ne $null) { |
| 35 | + # Write-Host "ApiKey $($obj.Value) already exists." |
| 36 | + $apiKeyExists = $true |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +# check if client exists |
| 42 | +$query = "SELECT * FROM [ClientApps] WHERE [ClientId] = '$($clientId)' AND [Authority] = '$authUrl' FOR JSON AUTO, WITHOUT_ARRAY_WRAPPER" |
| 43 | +$params = "exec", $DataContainer, "/opt/mssql-tools/bin/sqlcmd", "-d", $DataSource, "-U", $user, "-P", $password, "-Q", $query |
| 44 | +$result = Invoke-Cli -execFile "docker" -params $params -ErrorAction SilentlyContinue |
| 45 | +if ($result -ne "" -and $result -ne $null -and $result.Count -gt 1 -and $result[2] -ne $null) { |
| 46 | + $obj = $result[2] | ConvertFrom-Json |
| 47 | + if ($obj -ne $null) { |
| 48 | + # Write-Host "Client with ClientId $($obj.ClientId) already exists." |
| 49 | + $clientExists = $true |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +if ($apiKeyExists -and $clientExists) { |
| 54 | + Write-Host "Both Client with ClientId and ApiKey already exists." |
| 55 | + return |
| 56 | +} |
| 57 | + |
| 58 | +# default values |
| 59 | + |
| 60 | +$charSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" |
| 61 | +$rndmId = -join (1..16 | ForEach-Object { Get-Random -InputObject $charSet.ToCharArray() }) |
| 62 | +$creationDate = Get-Date -Format "yyyy-MM-dd HH:mm:ss.fffffff" |
| 63 | + |
| 64 | +# Check if apikey exists and insert or update accordingly |
| 65 | +$clientappScript = @" |
| 66 | +IF NOT EXISTS (SELECT 1 FROM [AccessTokens] WHERE [Value] = '$apiKey' AND [UserId] = 1) |
| 67 | +BEGIN |
| 68 | + INSERT INTO [AccessTokens] |
| 69 | + ([UserId], [Value], [Feature], [CreationDate], [ExpirationDate]) |
| 70 | + VALUES |
| 71 | + ('1', '$apiKey', 'apikey', '$creationDate', '9999-12-31 23:59:59.9999999') |
| 72 | +END |
| 73 | +"@ |
| 74 | + |
| 75 | +$params = "exec", $DataContainer, "/opt/mssql-tools/bin/sqlcmd", "-d", $DataSource, "-U", $user, "-P", $password, "-Q", $clientappScript |
| 76 | +Invoke-Cli -execFile "docker" -params $params -ErrorAction Stop |
| 77 | + |
| 78 | +# Check if clientapp exists and insert or update accordingly |
| 79 | +$clientappScript = @" |
| 80 | +IF EXISTS (SELECT 1 FROM [ClientApps] WHERE [ClientId] = '$clientId') |
| 81 | +BEGIN |
| 82 | + UPDATE [ClientApps] |
| 83 | + SET [Name] = '$clientId', [Repository] = '$repoUrl', [UserName] = 'builtin\admin', [Authority] = '$authUrl', [Type] = 4 |
| 84 | + WHERE [ClientId] = '$clientId' |
| 85 | +END |
| 86 | +ELSE |
| 87 | +BEGIN |
| 88 | + INSERT INTO [ClientApps] |
| 89 | + ([ClientId], [Name], [Repository], [UserName], [Authority], [Type]) |
| 90 | + VALUES |
| 91 | + ('$clientId', '$clientId', '$repoUrl', 'builtin\admin', '$authUrl', 4) |
| 92 | +END |
| 93 | +"@ |
| 94 | + |
| 95 | +$params = "exec", $DataContainer, "/opt/mssql-tools/bin/sqlcmd", "-d", $DataSource, "-U", $user, "-P", $password, "-Q", $clientappScript |
| 96 | +Invoke-Cli -execFile "docker" -params $params -ErrorAction Stop |
| 97 | + |
| 98 | +# Check if client secret exists and insert or update accordingly |
| 99 | +$clientsecretScript = @" |
| 100 | +IF EXISTS (SELECT 1 FROM [ClientSecrets] WHERE [ClientId] = '$clientId') |
| 101 | +BEGIN |
| 102 | + UPDATE [ClientSecrets] |
| 103 | + SET [Value] = '$clientSecret', [CreationDate] = '$creationDate', [ValidTill] = '9999-12-31 23:59:59.9999999' |
| 104 | + WHERE [ClientId] = '$clientId' |
| 105 | +END |
| 106 | +ELSE |
| 107 | +BEGIN |
| 108 | + INSERT INTO [ClientSecrets] |
| 109 | + ([Id], [ClientId], [Value], [CreationDate], [ValidTill]) |
| 110 | + VALUES |
| 111 | + ('$rndmId', '$clientId', '$clientSecret', '$creationDate', '9999-12-31 23:59:59.9999999') |
| 112 | +END |
| 113 | +"@ |
| 114 | + |
| 115 | +$params = "exec", $DataContainer, "/opt/mssql-tools/bin/sqlcmd", "-d", $DataSource, "-U", $user, "-P", $password, "-Q", $clientsecretScript |
| 116 | +Invoke-Cli -execFile "docker" -params $params -ErrorAction Stop |
| 117 | + |
| 118 | + |
0 commit comments