-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxnat-token.sh
executable file
·115 lines (85 loc) · 2.36 KB
/
xnat-token.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
#!/usr/bin/env bash
set -euo pipefail
warn() {
echo "$@" 1>&2
}
usage() {
cat <<EOF
xnat-token: Generates a new user token for XNAT
USAGE
xnat-token [OPTIONS]
OPTIONS
-d Connect to development XNAT
-h Displays this help message
-u Username for XNAT
EXAMPLES
xnat-token -u broarr
NOTES
XNAT tokens live for 48 hours before they're invalidated.
EOF
}
main() {
local url='https://xnat.bnc.brown.edu'
local username=''
local password=''
local token=''
local alias=''
local secret=''
while getopts ":dhu:" opt; do
case "${opt}" in
d)
url="https://qa-xnat.bnc.brown.edu"
;;
h)
usage; exit 0
;;
u)
username="${OPTARG}"
;;
\?)
warn "Invalid option: -${OPTARG}"
exit 1
;;
:)
warn "Invalid option: -${OPTARG} requires an argument"
exit 1
;;
esac
done
echo "Connecting to ${url}"
if [ -z "${username}" ]; then
echo -n "username: "
read -r username
fi
echo -n "password: "
read -sr password
echo
url="${url}/data/services/tokens/issue"
token="$(curl -s -u "${username}:${password}" "${url}" )"
# NOTE (BNR): Normally I'd be using jq for this, but jq is not available in
# oscar. Instead I stick to UNIX tools that should be available
# on every POSIX compliant machine.
alias="$(echo "${token}" | sed -n 's/.*"alias":"\([^"]*\)".*/\1/p')"
secret="$(echo "${token}" | sed -n 's/.*"secret":"\([^"]*\)".*/\1/p')"
cat <<EOF
The following token will be valid for 48 hours
Use \$XNAT_TOKEN in place of username and \$XNAT_SECRET in place of password
If you are using an interact session then you will need to export this variables
-------------------------------------------------------------------------------
export XNAT_TOKEN=${alias}
export XNAT_SECRET=${secret}
or
export XNAT_USER=${alias}
export XNAT_PASSWORD=${secret}
-------------------------------------------------------------------------------
If you are using an sbatch script, then you can define the variables in your script. For example
-------------------------------------------------------------------------------
XNAT_TOKEN=${alias}
XNAT_SECRET=${secret}
or
XNAT_USER=${alias}
XNAT_PASSWORD=${secret}
-------------------------------------------------------------------------------
EOF
}
main "$@"