-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish_jwt_orcid
executable file
·208 lines (162 loc) · 4.65 KB
/
publish_jwt_orcid
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/bin/bash
#
# Script to publish a token to a location retrievable
# by ORCID from https://project.dataone.org/o/{ORCID}.
#
VERSION="1.0.0"
APPNAME=$(basename ${0})
APPDIR=$(pwd)
TMPDIR="/tmp"
PUBLISH_HOST="project.dataone.org"
PUBLISH_PATH="/var/www/project/html/o"
VERBOSE=""
DRYRUN=""
USER_ID=""
TOKEN=""
function showVersion() {
echo ${VERSION}
}
function usage() {
cat << EOF
${APPNAME} version ${VERSION}
usage: ${APPNAME} OPTIONS ORCID TOKEN
scp the provided jwt token to a protected location where it can be retrieved by the specified user.
OPTIONS:
-h Show this message
-H Show this message with examples
-D Dry run. Show what will be done.
-v Show version and exit
-V Verbose output to STDERR
USER:
The ORCID that will be retrieving the token.
TOKEN:
Path to the file for the token (typically ending *.jwt).
EOF
}
function usageExamples() {
usage
cat << EOF
Example - Share a token for user vieglais:
./${APPNAME} 0000-0002-6513-4996 myToken.jwt
EOF
}
function log() {
if [[ ! -z ${VERBOSE} ]]; then
echo "LOG: $@" 1>&2;
fi
}
function lwarn() {
echo "WARN: $@" 1>&2;
}
function lerror() {
echo "ERROR: $@" 1>&2;
}
function shareToken() {
#create a safe name
log "Token = ${TOKEN}"
local _tokenName=$(basename -s ".jwt" "${TOKEN}")
local _tokenPath=$(dirname "${TOKEN}")
#replace : with _
local _safename=${_tokenName//[:]/_}
#Create a file
local _tmp_dir=${TMPDIR}/${_safename}
if [[ -d ${_tmp_dir} ]]; then
lerror "Temporary folder already exists: ${_tmp_dir} Remove to continue."
exit 1;
fi
if [[ -z ${DRYRUN} ]]; then
mkdir -p "${_tmp_dir}/private"
if [ "$?" -ne "0" ]; then
lerror "failed: mkdir -p \"${_tmp_dir}/private\""
exit 1
fi
cp ${TOKEN} "${_tmp_dir}/private/${_safename}.jwt"
cat > "${_tmp_dir}/README.txt" << EOF
README
======
This folder contains a JSON Web Token (*.jwt) file, that can be used to authenticate a user or
service within the DataONE environment.
Contents::
${_safename}/
README.txt
private/${_safename}.jwt
Please back up the token file to offline media (encrypted thumbdrive, etc.) where
it cannot be read except by the Member Node administrator. This token allows
anyone with access to it, to operate on any files created by the Member Node
in the DataONE system, and therefore needs to be stored safely. If the file is
compromised, please send an email to [email protected] and we will revoke
the token and reissue a new one to the Member Node administrator.
Warning
-------
It is important that the file::
private/${_safename}.jwt
is kept secure, as it identifies anyone using it as the user or service for which it was produced.
Metadata About Token
--------------------
EOF
cat ${_tmp_dir}/private/${_safename}.jwt | awk -F. '{print $2}' | base64 -d >> "${_tmp_dir}/README.txt"
fi
local _ZIPFILE="${TMPDIR}/${_safename}.zip"
local CDIR=$(pwd)
cd ${TMPDIR}
log "zip -r ${_ZIPFILE} ${_safename}"
if [[ -z ${DRYRUN} ]]; then
if [[ ! -z ${VERBOSE} ]]; then
zip -r ${_ZIPFILE} ${_safename}
else
zip -q -r ${_ZIPFILE} ${_safename}
fi
if [ "$?" -ne "0" ]; then
lerror "Zip creation failed."
lwarn "Aborting."
cd ${CDIR}
rm -rf ${_tmp_dir}
rm ${_ZIPFILE}
exit 1
fi
fi
cd ${CDIR}
#scp the files, creating a folder if needed
log "Copy to host..."
log "cat ${_ZIPFILE} | ssh ${PUBLISH_HOST} \"mkdir -p ${PUBLISH_PATH}/${USER_ID};cat > ${PUBLISH_PATH}/${USER_ID}/\$(basename ${_ZIPFILE})\""
if [[ -z ${DRYRUN} ]]; then
cat ${_ZIPFILE} | ssh ${PUBLISH_HOST} "mkdir -p ${PUBLISH_PATH}/${USER_ID};cat > ${PUBLISH_PATH}/${USER_ID}/$(basename ${_ZIPFILE})"
#clean up
rm -rf ${_tmp_dir}
#rm ${_ZIPFILE}
echo ${_ZIPFILE}
fi
echo
echo "Upload successful."
echo "The package can be retrieved from:"
echo " https://${PUBLISH_HOST}/o/${USER_ID}/$(basename ${_ZIPFILE})"
echo
}
# === Main ===
while getopts "hHvVPdDr:" OPTION
do
case ${OPTION} in
h) usage; exit 1;;
H) usageExamples; exit 1;;
v) showVersion; exit 1;;
V) VERBOSE=1;;
D) DRYRUN=1;;
\?) usage; exit 1;;
esac
done
shift $((OPTIND-1))
USER_ID=${1}
TOKEN=${2}
if [[ -z ${USER_ID} ]]; then
echo "User ID is required."
exit 1;
fi
if [[ -z ${TOKEN} ]]; then
echo "Token file name is required."
exit 1;
fi
if [[ ! -f ${TOKEN} ]]; then
lerror " File not found: ${TOKEN}"
exit 1;
fi
shareToken