-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken-generator.sh
executable file
Β·128 lines (102 loc) Β· 3.24 KB
/
token-generator.sh
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
#!/usr/bin/env bash
# https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux
Cyan='\033[0;36m' # Cyan
Red='\033[0;31m' # Red
Purple='\033[0;35m' # Purple
Yellow='\033[0;33m' # Yellow
BPurple='\033[1;35m' # Purple bold
UGreen='\033[4;32m' # Green underline
# Env file
envFile='.env'
# json config
jsonConfig='token-generator.config.json'
# Main script
init() {
# Welcome text
echo -e "${Cyan}::: ${BPurple}dp-saksbehandling-frontend token generator ${Cyan}::: \n"
# Check if jq package is installed
verifyJQ
# Generate azure-token-generator token
startTokenGenerator
# Finished
sleep 1
echo -e "π ${Purple}You're good to go! Restart your dev-server."
}
# Check if user has `jq` installed
# https://formulae.brew.sh/formula/jq
verifyJQ() {
if brew ls --versions jq > /dev/null; then
# jq already installed, continue script
:
else
# jq not found
# ask user to install jq
echo -e "${Yellow}π‘ jq not found. jq is required for token-generator script."
echo -e "${Yellow}π‘ Read more about jq: ${UGreen}https://formulae.brew.sh/formula/jq${Cyan}\n"
# ask for user input y or n
read -p "Install jq (y/n)? " answer
if [ "$answer" = "y" ]; then
brew install jq
echo -e "\n"
else
echo -e "π ${Red}Token generator aborted."
exit 1
fi
fi
}
# Start token generation process
startTokenGenerator() {
# First azure-token-generator url from json config
url=$(jq '.' $jsonConfig | jq '.[0].url' | tr -d '"')
# Show link to azureTokenGenerator to user
echo -e "${Cyan}Visit: ${UGreen}${url}\n"
echo -e "${Cyan}Find and copy ${Yellow}io.nais.wonderwall.session ${Cyan}cookie from ${Yellow}DevTools > Application > Cookies"
# Ask for wonderwall cookie,
echo -e "${Cyan}Paste in cookie: "
read cookie
echo -e "\n"
configArray=$(jq -r '.[] | @base64' $jsonConfig)
# Loop through config list and create environment variable
for config in $configArray;
do
_jq() {
echo ${config} | base64 --decode | jq -r ${1}
}
env=$(_jq '.env')
url=$(_jq '.url')
generateAndUpdateEnvFile "$env" "$url" "$cookie"
done
echo -e "\n"
}
# This function make curl request with `-b` flag to send cookie with the request
# | jq ".access_token" returns access_token string
generateAndUpdateEnvFile() {
# function parameters
env=$1
url=$2 | tr -d '"'
cookie=$3
# Add env key if not exits
# Example: DP_SAKSBEHANDLING_TOKEN
if grep -q "$env" "$envFile"; then
# env already exits, continue script
:
else
# Add missing env key
printf "%s\n" '$a' "${env}" . w | ed -s "$envFile"
fi
# Store access token in variable
accessToken=$(curl -s -b "io.nais.wonderwall.session=${cookie}" ${url}| jq ".access_token")
# Check if accessToken is empty or null
if [[ -z "$accessToken" || "$accessToken" == null ]]; then
echo -e "β ${Yellow}${env} ${Red} error"
exit 1
else
# Fully generated env string
generatedEnv="${env}=${accessToken}"
# Update generated env string to env file
printf '%s\n' H ",g/^${env}.*/s//${generatedEnv}/" wq | ed -s "$envFile"
echo -e "β
${Yellow}${env} ${Cyan}updated"
fi
}
# Start script
init