-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMAC-androidv2tutorial.ps1
188 lines (150 loc) · 5.52 KB
/
MAC-androidv2tutorial.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
param(
[string]$appName,
[string]$packageName,
[string]$signatureHash
)
# check if signing key is provided
if (-not $signatureHash) {
# Run the signingReport task
$gradleOutput = ./gradlew signingReport
# Extract the SHA-1 key
$sha1Regex = "(?<=SHA1:\s)([0-9A-Fa-f]{2}(?::[0-9A-Fa-f]{2}){19})"
$sha1Hashes = [regex]::Matches($gradleOutput, $sha1Regex)
if ($sha1Hashes.Count -gt 0) {
$sha1Key = $sha1Hashes[0].Value
$sanitizedSha1 = $sha1Key -replace '\W'
$bytes = [byte[]] -split ($sanitizedSha1 -replace '..', '0x$& ')
$base64Sha1 = [System.Convert]::ToBase64String($bytes)
} else {
Write-Host "Error: Unable to find SHA-1 key in the signing report."
}
Write-Host "Base64 SHA-1: $base64Sha1"
$signatureHash = $base64Sha1
}
# Check if Azure CLI is installed
function Test-AzCLIInstalled {
try {
az --version | Out-Null
return $true
} catch {
return $false
}
}
function Test-AzCLIInstalled {
try {
az --version | Out-Null
return $true
} catch {
return $false
}
}
function Test-AzCLIInstalled {
try {
az --version | Out-Null
return $true
} catch {
return $false
}
}
# Install CLI if not installed
if (-not (Test-AzCLIInstalled)) {
Write-Host "Azure CLI not found. Attempting to download and install..."
$ProgressPreference = 'SilentlyContinue'
try {
# Ensure Homebrew is installed
if (-not (Test-Path "/usr/local/bin/brew")) {
Write-Host "Homebrew is not installed. Installing Homebrew first..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
}
# Install Azure CLI using Homebrew
brew update
brew install azure-cli
# Verify installation
if (-not (Test-AzCLIInstalled)) {
throw "Azure CLI installation failed."
}
Write-Host "Azure CLI installed successfully."
} catch {
Write-Host "Error downloading or installing Azure CLI: $_" -ForegroundColor Red
exit
}
} else {
Write-Host "Azure CLI is already installed."
}
# Generates a redirect URI
$redirectUri = "msauth://$packageName/$signatureHash"
# Login to Azure
Write-Host "Please Log in to Azure..."
az login --tenant TENANT_ID
# Create the app registration
$appRegistration = az ad app create --display-name $appName
# Extract the App (Client) ID
$appId = $appRegistration.appId
# add redirect URI to the app registration
$app = az ad app list --display-name $appName --query "[].{id:appId}" --output tsv
$appId = $app.Trim()
az ad app update --id $appId --public-client-redirect-uris $redirectUri
Write-Host "App registration updated with redirect URI"
# Generate MSAL Config file
$configJson = @{
"client_id" = "$appId"
"authorization_user_agent" = "WEBVIEW"
"redirect_uri" = "$redirectUri"
"account_mode" = "MULTIPLE"
"broker_redirect_uri_registered" = $true
"authorities" = @(
@{
"type" = "AAD"
"authority_url" = "https://login.microsoftonline.com/common"
"default" = $true
}
)
} | ConvertTo-Json -Depth 10
# check if folder .\app\src\main\res\raw exists otherwise create it
if (-not (Test-Path -Path "app/src/main/res/raw")) {
New-Item -Path "./app/src/main/res/raw" -ItemType File
}
$configFilePath = "./app/src/main/res/raw/auth_config_multiple_account.json"
Write-Host "MSAL config file generated at $configFilePath"
# Generate MSAL Helper file
$msalHelper = @"
package $packageName
import android.app.Activity
import android.content.Context
import com.microsoft.identity.client.AcquireTokenParameters
import com.microsoft.identity.client.AuthenticationCallback
import com.microsoft.identity.client.IMultipleAccountPublicClientApplication
import com.microsoft.identity.client.IPublicClientApplication
import com.microsoft.identity.client.Prompt
import com.microsoft.identity.client.PublicClientApplication
import com.microsoft.identity.client.exception.MsalException
object MsalHelper {
private var sApplication: IPublicClientApplication? = null
private fun createApplication(context: Context) {
PublicClientApplication.createMultipleAccountPublicClientApplication(context,
R.raw.auth_config_multiple_account,
object : IPublicClientApplication.IMultipleAccountApplicationCreatedListener {
override fun onCreated(application: IMultipleAccountPublicClientApplication) {
sApplication = application;
}
override fun onError(exception: MsalException) {}
})
}
fun acquireToken(activity: Activity, callback: AuthenticationCallback) {
if (sApplication == null) {
createApplication(activity)
}
val parameters: AcquireTokenParameters = AcquireTokenParameters.Builder()
.startAuthorizationFromActivity(activity)
.fromAuthority("https://login.microsoftonline.com/08af321a-c895-4f78-afbd-6cede994e80f")
.withPrompt(Prompt.SELECT_ACCOUNT)
.withScopes(listOf("user.read"))
.withCallback(callback)
.build()
sApplication?.acquireToken(parameters)
}
}
"@
$msalhelperPath = ".\app\src\main\java\" + $packageName.replace(".", "\") + "\MsalHelper.kt"
$msalHelper | Out-File -FilePath $msalhelperPath -Encoding utf8
Write-Host "MSAL helper file generated at $msalhelperPath"