Project

General

Profile

Download (3.56 KB) Statistics
| Branch: | Tag: | Revision:

haketilo / upload_amo.sh @ d7e48c58

1
#!/bin/sh
2

    
3
# This file is part of Haketilo
4
#
5
# Copyright (C) 2021, Wojtek Kosior
6
#
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
#
20
#
21
# I, Wojtek Kosior, thereby promise not to sue for violation of this file's
22
# license. Although I request that you do not make use this code in a
23
# proprietary program, I am not going to enforce this in court.
24

    
25
set -e
26

    
27
. ./shell_utils.sh
28

    
29
_PROG_NAME="$0"
30
OPERATION="$1"
31
API_KEY="$2"
32
SECRET="$3"
33
XPI_PATH="$4"
34

    
35
base64url() {
36
    ech "$1" | base64 -w 0 | tr '/+' '_-' | tr -d '='
37
}
38

    
39
sha256hmac() {
40
    base64url "$(ech "$2" | openssl dgst -sha256 -hmac "$1" -binary -)"
41
}
42

    
43
escape_regex_special() {
44
    ech "$1" | sed 's/\([]\.*?{},()[-]\)/\\\1/g'
45
}
46

    
47
# Note: We don't actually parse JSON. We extract needed keys with sed regexes
48
# which does not work in the general case but is sufficient for now.
49
get_json_key() {
50
    local KEY_REG="$(escape_regex_special "$1")"
51
    ech "$2" |
52
	sed 's/\(.*"'"$KEY_REG"'"[[:space:]]*:[[:space:]]*"\([^"]*\)"\)\?.*/\2/' |
53
	grep . | head -1
54
}
55

    
56
get_manifest_key() {
57
    get_json_key "$1" "$(unzip -p "$2" manifest.json)"
58
}
59

    
60
generate_jwt() {
61
    local JWT_HEAD='{"alg":"HS256", "typ":"JWT"}'
62
    local JWT_ID=$(dd if=/dev/random bs=21 count=1 2>/dev/null | base64)
63
    local ISSUED_AT_TIME=$(date -u +%s)
64
    local EXPIRATION_TIME=$((ISSUED_AT_TIME + 300))
65
    local JWT_PAYLOAD="$(cat <<EOF
66
{
67
    "iss": "$API_KEY",
68
    "jti": "$JWT_ID",
69
    "iat": $ISSUED_AT_TIME,
70
    "exp": $EXPIRATION_TIME
71
}
72
EOF
73
	  )"
74
    local JWT_MESSAGE=$(base64url "$JWT_HEAD").$(base64url "$JWT_PAYLOAD")
75
    local JWT_SIGNATURE=$(sha256hmac "$SECRET" "$JWT_MESSAGE")
76
    local JWT=$JWT_MESSAGE.$JWT_SIGNATURE
77
    errcho "Using JWT: $JWT"
78
    ech $JWT
79
}
80

    
81
get_extension_url() {
82
    EXTENSION_ID="$(get_manifest_key id "$XPI_PATH")"
83
    EXTENSION_VER="$(get_manifest_key version "$XPI_PATH")"
84

    
85
    if [ -z "$EXTENSION_ID" -o -z "$EXTENSION_VER" ]; then
86
	errcho "Couldn't extract extension id and version. Please check if $XPI_PATH contains proper manifest.json file."
87
	exit 1
88
    fi
89

    
90
    ech "https://addons.mozilla.org/api/v4/addons/$EXTENSION_ID/versions/$EXTENSION_VER/"
91
}
92

    
93
usage() {
94
   errcho "Usage:  $_PROG_NAME upload|check|test API_KEY SECRET XPI_PATH"
95
}
96

    
97
if [ $# != 4 ]; then
98
    usage
99
    exit 1
100
fi
101

    
102
unset RETURNED_DATA
103

    
104
case "$OPERATION" in
105
    test)
106
	curl "https://addons.mozilla.org/api/v4/accounts/profile/" \
107
	     -g -H "Authorization: JWT $(generate_jwt)"
108
	echo
109
	;;
110
    check)
111
	RETURNED_DATA="$(curl $(get_extension_url) \
112
			      -g -H "Authorization: JWT $(generate_jwt)")"
113
	;;
114
    upload)
115
	RETURNED_DATA="$(curl $(get_extension_url) \
116
			      -g -XPUT --form "upload=@$XPI_PATH" \
117
			      -H "Authorization: JWT $(generate_jwt)")"
118
	;;
119
    *)
120
	usage
121
	exit 1
122
	;;
123
esac
124

    
125
if [ -n "$RETURNED_DATA" ]; then
126
    printf "addons.mozilla.org says:\n%s\n" "$RETURNED_DATA"
127
    DOWNLOAD_URL="$(get_json_key download_url "$RETURNED_DATA")"
128
    if [ -n "$DOWNLOAD_URL" ]; then
129
	printf "Downloading extension file from %s\n" "$DOWNLOAD_URL"
130
	curl "$DOWNLOAD_URL" -g -H "Authorization: JWT $(generate_jwt)" -O
131
    fi
132
fi
(9-9/9)