aboutsummaryrefslogtreecommitdiff
#!/bin/sh

# This file is part of Haketilo
#
# Copyright (C) 2021, Wojtek Kosior
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the CC0 1.0 Universal License as published by
# the Creative Commons Corporation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# CC0 1.0 Universal License for more details.

set -e

_PROG_NAME="$0"
OPERATION="$1"
API_KEY="$2"
SECRET="$3"
XPI_PATH="$4"

escape_regex_special() {
    printf %s "$1" | sed 's/\([]\.*[-]\)/\\\1/g'
}

# Note: We don't actually parse JSON. We extract needed keys with sed regexes
# which does not work in the general case but is sufficient for now.
_get_json_key() {
    local KEY_REG="$(escape_regex_special "$1")"
    printf %s "$2" |
	awk '{printf "%s", $0}' |
	sed 's/^.*\("'"$KEY_REG"'"[[:space:]]*:[[:space:]]*"\([^"]*\)"\).*$/\2/'
}

get_json_key() {
    local JSON="$2"
    local VALUE="$(_get_json_key "$@")"
    if [ "x$VALUE" != "x$JSON" ]; then
	printf %s "$VALUE"
    fi
}

base64url() {
    printf %s "$1" | base64 -w 0 | tr '/+' '_-' | tr -d '='
}

sha256hmac() {
    base64url "$(printf %s "$2" | openssl dgst -sha256 -hmac "$1" -binary -)"
}

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

generate_jwt() {
    local JWT_HEAD='{"alg":"HS256", "typ":"JWT"}'
    local JWT_ID=$(dd if=/dev/random bs=21 count=1 2>/dev/null | base64)
    local ISSUED_AT_TIME=$(date -u +%s)
    local EXPIRATION_TIME=$((ISSUED_AT_TIME + 300))
    local JWT_PAYLOAD="$(cat <<EOF
{
    "iss": "$API_KEY",
    "jti": "$JWT_ID",
    "iat": $ISSUED_AT_TIME,
    "exp": $EXPIRATION_TIME
}
EOF
	  )"
    local JWT_MESSAGE=$(base64url "$JWT_HEAD").$(base64url "$JWT_PAYLOAD")
    local JWT_SIGNATURE=$(sha256hmac "$SECRET" "$JWT_MESSAGE")
    local JWT=$JWT_MESSAGE.$JWT_SIGNATURE
    printf "Using JWT: $JWT\n" >&2
    printf $JWT
}

get_extension_url() {
    EXTENSION_ID="$(get_manifest_key id "$XPI_PATH")"
    EXTENSION_VER="$(get_manifest_key version "$XPI_PATH")"

    if [ -z "$EXTENSION_ID" -o -z "$EXTENSION_VER" ]; then
	printf "Couldn't extract extension id and version. Please check if %s contains proper manifest.json file.\n" \
	       "$XPI_PATH" >&2
	exit 1
    fi

    printf 'https://addons.mozilla.org/api/v4/addons/%s/versions/%s/' \
	   "$EXTENSION_ID" "$EXTENSION_VER"
}

print_usage() {
    printf 'Usage:  %s upload|check|test API_KEY SECRET XPI_PATH\n' \
	   "$_PROG_NAME" >&2
}

if [ $# != 4 ]; then
    print_usage
    exit 1
fi

unset RETURNED_DATA

case "$OPERATION" in
    test)
	curl "https://addons.mozilla.org/api/v4/accounts/profile/" \
	     -g -H "Authorization: JWT $(generate_jwt)"
	printf '\n'
	;;
    check)
	RETURNED_DATA="$(curl $(get_extension_url) \
			      -g -H "Authorization: JWT $(generate_jwt)")"
	;;
    upload)
	RETURNED_DATA="$(curl $(get_extension_url) \
			      -g -XPUT --form "upload=@$XPI_PATH" \
			      -H "Authorization: JWT $(generate_jwt)")"
	;;
    *)
	print_usage
	exit 1
	;;
esac

if [ -n "$RETURNED_DATA" ]; then
    printf "addons.mozilla.org says:\n%s\n" "$RETURNED_DATA"
    DOWNLOAD_URL="$(get_json_key download_url "$RETURNED_DATA")"
    if [ -n "$DOWNLOAD_URL" ]; then
	printf "Downloading extension file from %s\n" "$DOWNLOAD_URL"
	curl "$DOWNLOAD_URL" -g -H "Authorization: JWT $(generate_jwt)" -O
    fi
fi
44'>maint: Avoid macros obsolete in Autoconf 2.70....Ludovic Courtès 2020-11-29Make "guile-avahi" dependency optional....Mathieu Othacehe 2020-11-29Add Avahi support....Mathieu Othacehe 2020-10-22git: Require Guile-Git 0.3.0 or later....Ludovic Courtès 2020-10-05build: Use a 'guile' executable that doesn't warn about locales....Ludovic Courtès 2020-08-29build: Remove references to the 'nix-hash' program....Ludovic Courtès 2020-08-29build: Remove check for Guile 2.2.1 bug....Ludovic Courtès 2020-08-24Use "guile-zlib" and "guile-lzlib" instead of (guix config)....Mathieu Othacehe 2020-06-16etc: Add committer script....Ricardo Wurmus 2020-05-28maint: Check whether Guile-Gcrypt is recent enough....Ludovic Courtès 2020-05-14etc: indent-code.el: Use the --quick option....Maxim Cournoyer 2020-04-23build: Don't build man pages when cross-compiling....Ludovic Courtès 2020-03-11build: Require Guile 2.2.3 or later....Ludovic Courtès 2020-01-17build: Allow builds with Guile 3.0....Ludovic Courtès 2019-11-09build: Warn about etc/indent-code.el when Emacs is absent....zimoun 2019-07-25maint: Switch to Guile-JSON 3.x....Ludovic Courtès 2019-05-06Add (guix lzlib)....Pierre Neidhardt 2019-04-17build: No longer substitute 'LIBGCRYPT'....Ludovic Courtès 2019-01-17configure: Disable installer build by default....Mathieu Othacehe 2019-01-17gnu: Add graphical installer support....Mathieu Othacehe 2019-01-09build: Require Guile 2.2....Ludovic Courtès 2018-12-02Make Guile-JSON a required dependency....Eric Bavier 2018-10-12build: Fine-tune the forbidden M4 patterns....Ludovic Courtès 2018-10-12build: Remove obsolete reference to 'GUIX_ASSERT_LIBGCRYPT_USABLE'....Ludovic Courtès 2018-09-04Switch to Guile-Gcrypt....Ludovic Courtès 2018-06-18build: Remove checks for 'nix-instantiate'....Ludovic Courtès 2018-06-14build: Require Guile-SQLite3....Ludovic Courtès 2018-06-01build: Check for Guile-SQLite3....Ludovic Courtès 2018-04-19gnu: doc: Allow documentation to be translated....Julien Lepiller