-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsetup.ps1
126 lines (104 loc) · 4.42 KB
/
setup.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
$nl = [Environment]::NewLine
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
[Security.Principal.WindowsBuiltInRole] "Administrator"))
{
Write-Host "You do not have Administrator rights to run this script!`nPlease re-run this script as an Administrator!"
Write-Host $nl"Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Break
}
# Set black background
$Host.UI.RawUI.BackgroundColor = "Black"
Clear-Host
# define some variables
$temp="c:\Temp\GoogleAuthSetup-yFH4gu"
$npm="npm-1.4.12.zip"
$config="c:\Program Files\Qlik\Sense\ServiceDispatcher"
$target="$config\Node\Google-Auth"
# check if module is installed
if(!(Test-Path -Path "$target\node_modules")) {
$confirm = Read-Host "This script will install the Google Auth module, do you want to proceed? [Y/n]"
if ($confirm -eq 'n') {
Break
}
# check if npm has been downloaded already
if(!(Test-Path -Path "$temp\$npm")) {
New-Item -Path "$temp" -Type directory -force | Out-Null
Invoke-WebRequest "http://nodejs.org/dist/npm/$npm" -OutFile "$temp\$npm"
}
# check if module has been downloaded
if(!(Test-Path -Path "$target\src")) {
New-Item -Path "$target\src" -Type directory | Out-Null
Invoke-WebRequest "http://raw.githubusercontent.com/braathen/qlik-auth-google/master/service.js" -OutFile "$target\src\service.js"
Invoke-WebRequest "http://raw.githubusercontent.com/braathen/qlik-auth-google/master/package.json" -OutFile "$target\package.json"
}
# check if npm has been unzipped already
if(!(Test-Path -Path "$temp\node_modules")) {
Write-Host "Extracting files..."
Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::ExtractToDirectory("$temp\$npm", "$temp")
}
# install module with dependencies
Write-Host "Installing modules..."
Push-Location "$target\src"
$env:Path=$env:Path + ";$config\Node"
&$temp\npm.cmd config set spin=false
&$temp\npm.cmd --prefix "$target" install
Pop-Location
# cleanup temporary data
Write-Host $nl"Removing temporary files..."
Remove-Item $temp -recurse
}
function Read-Default($text, $defaultValue) { $prompt = Read-Host "$($text) [$($defaultValue)]"; return ($defaultValue,$prompt)[[bool]$prompt]; }
# check if config has been added already
if (!(Select-String -path "$config\services.conf" -pattern "Identity=rfn-google-auth" -quiet)) {
$settings = @"
[google-auth]
Identity=rfn-google-auth
Enabled=true
DisplayName=Google Auth
ExecType=nodejs
ExePath=Node\node.exe
Script=Node\google-auth\src\service.js
[google-auth.parameters]
domain=
user_directory=
client_id=
client_secret=
redirect_uris=
"@
Add-Content "$config\services.conf" $settings
}
# look for client secret json file from Google
if((Test-Path -Path "client_secret*.json")) {
$json = Get-Content -Raw -Path client_secret*.json | ConvertFrom-Json
$client_id = $json.web.client_id
$client_secret = $json.web.client_secret
$redirect_uri = $json.web.redirect_uris[0]
}
# configure module
Write-Host $nl"CONFIGURE MODULE"
Write-Host $nl"To make changes to the configuration in the future just re-run this script."
$domain=Read-Default $nl"Enter domain for valid email addresses" "gmail.com"
$user_directory=Read-Default $nl"Enter name of user directory" "GOOGLE"
$client_id=Read-Default $nl"ClientID" $client_id
$client_secret=Read-Default $nl"Client Secret" $client_secret
$redirect_uri=Read-Default $nl"Redirect URI" $redirect_uri
function Set-Config( $file, $key, $value )
{
$regreplace = $("(?<=$key).*?=.*")
$regvalue = $("=" + $value)
if (([regex]::Match((Get-Content $file),$regreplace)).success) {
(Get-Content $file) `
|Foreach-Object { [regex]::Replace($_,$regreplace,$regvalue)
} | Set-Content $file
}
}
# write changes to configuration file
Write-Host $nl"Updating configuration..."
Set-Config -file "$config\services.conf" -key "domain" -value $domain
Set-Config -file "$config\services.conf" -key "user_directory" -value $user_directory
Set-Config -file "$config\services.conf" -key "client_id" -value $client_id
Set-Config -file "$config\services.conf" -key "client_secret" -value $client_secret
Set-Config -file "$config\services.conf" -key "redirect_uris" -value $redirect_uri
Write-Host $nl"Done! Please restart the 'Qlik Sense Service Dispatcher' service for changes to take affect."$nl