Skip to content

Commit

Permalink
feat: adapt to fib-jws@>=2.
Browse files Browse the repository at this point in the history
- keep fib-session's behavior against change from [break in fib-jws](fibjs/fib-jws@9657a1b)
  • Loading branch information
richardo2016 committed Apr 17, 2024
1 parent 530e15b commit aaef3b1
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 51 deletions.
74 changes: 74 additions & 0 deletions .github/workflows/fns.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/bin/bash

vercomp () {
if [[ $1 == $2 ]]; then
export vercomp_last_result=0
return $vercomp_last_result
fi
local IFS=.
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++))
do
if [[ -z ${ver2[i]} ]]; then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]})); then
export vercomp_last_result=1
return $vercomp_last_result
fi
if ((10#${ver1[i]} < 10#${ver2[i]})); then
export vercomp_last_result=2
return $vercomp_last_result
fi
done
export vercomp_last_result=0
return $vercomp_last_result
}

install_fibjs() {
local version=$1
if [[ -z "$version" ]]; then
echo "[install_fibjs] version is required"
exit 1
fi
local os=$2
if [[ -z "$os" ]]; then
echo "[install_fibjs] os is required"
exit 1
fi
local arch=$3
if [[ -z "$arch" ]]; then
echo "[install_fibjs] arch is required"
exit 1
fi

local url_base="https://github.com/fibjs/fibjs/releases/download/v${version}/fibjs-v${version}-${os}-${arch}"

# in fact, there's also non-archived linux fibjs
if [[ "$RUNNER_OS" == "Linux" ]]; then
if [ "$lower_than_0_37_0" == "true" ]; then
local remote_url="${url_base}.xz"
curl -SL "$remote_url" -o ./node_modules/.bin/fibjs.xz;
xz -d ./node_modules/.bin/fibjs.xz;
else
local remote_url="${url_base}.tar.gz"
curl -SL "$remote_url" -o ./node_modules/.bin/fibjs.tar.gz;
tar -xzf ./node_modules/.bin/fibjs.tar.gz -C ./node_modules/.bin;
fi
chmod a+x ./node_modules/.bin/fibjs;
elif [[ "$RUNNER_OS" == "macOS" ]]; then
local remote_url="${url_base}"
curl -SL "$remote_url" -o ./node_modules/.bin/fibjs;
chmod a+x ./node_modules/.bin/fibjs;
else
local remote_url="${url_base}.exe"
curl -SL "$remote_url" -o ./node_modules/.bin/fibjs.exe;
fi
echo "[install_fibjs] Downloading fibjs from ${remote_url}"
}
103 changes: 83 additions & 20 deletions .github/workflows/run-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,83 @@ jobs:
continue-on-error: true
strategy:
matrix:
os: [ubuntu-18.04, windows-2019, macos-10.15]
version: [0.33.0, 0.34.0, 0.35.0]
arch: [amd64, i386]
exclude:
- os: windows-2019
arch: i386
- os: macos-10.15
arch: i386
include:
- os: windows-2019
arch: x64
fibjs: 0.33.0
- os: windows-2019
arch: x64
fibjs: 0.34.0
- os: windows-2019
arch: x64
fibjs: 0.35.0
- os: windows-2019
arch: x64
fibjs: 0.36.0
- os: windows-2019
arch: x64
fibjs: 0.37.0
- os: windows-2019
arch: x86
fibjs: 0.33.0
- os: windows-2019
arch: x86
fibjs: 0.34.0
- os: windows-2019
arch: x86
fibjs: 0.35.0
- os: windows-2019
arch: x86
fibjs: 0.36.0
- os: windows-2019
arch: x86
fibjs: 0.37.0
- os: ubuntu-20.04
arch: x64
fibjs: 0.33.0
- os: ubuntu-20.04
arch: x64
fibjs: 0.34.0
- os: ubuntu-20.04
arch: x64
fibjs: 0.35.0
- os: ubuntu-20.04
arch: x64
fibjs: 0.36.0
- os: ubuntu-20.04
arch: x64
fibjs: 0.37.0
- os: ubuntu-20.04
arch: x86
fibjs: 0.33.0
- os: ubuntu-20.04
arch: x86
fibjs: 0.34.0
- os: ubuntu-20.04
arch: x86
fibjs: 0.35.0
- os: ubuntu-20.04
arch: x86
fibjs: 0.36.0
- os: ubuntu-20.04
arch: x86
fibjs: 0.37.0
- os: macos-11
arch: x64
fibjs: 0.33.0
- os: macos-11
arch: x64
fibjs: 0.34.0
- os: macos-11
arch: x64
fibjs: 0.35.0
- os: macos-11
arch: x64
fibjs: 0.36.0
- os: macos-11
arch: x64
fibjs: 0.37.0


steps:
- name: Check out Git repository
Expand All @@ -41,7 +110,7 @@ jobs:

- uses: actions/setup-node@v2
with:
node-version: '14'
node-version: '16'

- name: Set Env Variables
id: set-env-vars
Expand All @@ -51,26 +120,20 @@ jobs:
env:
ARCH: ${{ matrix.arch }}
OS: ${{ matrix.os }}
FIBJS_VERSION: ${{ matrix.fibjs }}

- name: Install FIBJS
shell: bash
run: |
mkdir -p ./node_modules/.bin;
rm -rf ./node_modules/.bin/fibjs;
if [[ "$RUNNER_OS" == "Linux" ]]; then
curl -SL "https://github.com/fibjs/fibjs/releases/download/v${FIBJS_VERSION}/fibjs-v${FIBJS_VERSION}-${FIBJS_OS}-${FIBJS_ARCH}.xz" -o ./node_modules/.bin/fibjs.xz;
xz -d ./node_modules/.bin/fibjs.xz;
chmod a+x ./node_modules/.bin/fibjs;
elif [[ "$RUNNER_OS" == "macOS" ]]; then
curl -SL "https://github.com/fibjs/fibjs/releases/download/v${FIBJS_VERSION}/fibjs-v${FIBJS_VERSION}-${FIBJS_OS}-${FIBJS_ARCH}" -o ./node_modules/.bin/fibjs;
chmod a+x ./node_modules/.bin/fibjs;
else
curl -SL "https://github.com/fibjs/fibjs/releases/download/v${FIBJS_VERSION}/fibjs-v${FIBJS_VERSION}-${FIBJS_OS}-${FIBJS_ARCH}.exe" -o ./node_modules/.bin/fibjs.exe;
fi
. ./.github/workflows/fns.sh --source-only
install_fibjs $FIBJS_VERSION $FIBJS_OS $FIBJS_ARCH;
env:
FIBJS_OS: ${{ steps.set-env-vars.outputs.FIBJS_OS }}
FIBJS_ARCH: ${{ steps.set-env-vars.outputs.FIBJS_ARCH }}
FIBJS_VERSION: ${{ matrix.version }}
lower_than_0_37_0: ${{ steps.set-env-vars.outputs.lower_than_0_37_0 }}
FIBJS_VERSION: ${{ matrix.fibjs }}

- name: Run CI
shell: bash
Expand Down
28 changes: 24 additions & 4 deletions .github/workflows/set-env-vars.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
. ./.github/workflows/fns.sh --source-only

export GIT_BRANCH=${GITHUB_REF#refs/heads/}
echo "::set-output name=GIT_BRANCH::$GIT_BRANCH"
export GIT_TAG=$(git tag | grep $(git describe --tags HEAD))
Expand Down Expand Up @@ -25,9 +27,23 @@ if [ -z "$IS_GIT_TAG_MATCH_SEMVER" ]; then
fi
echo "::set-output name=RELEASE_TAG::$RELEASE_TAG";

vercomp "${FIBJS_VERSION}" "0.37.0"
if [[ "$vercomp_last_result" -eq "2" ]]; then
export lower_than_0_37_0="true"
else
export lower_than_0_37_0="false"
fi

echo "::set-output name=lower_than_0_37_0::$lower_than_0_37_0";

case "${RUNNER_OS}" in
Windows)
export FIBJS_OS=windows
# lower than 0.37.0
if [[ "$lower_than_0_37_0" == "true" ]]; then
export FIBJS_OS=windows
else
export FIBJS_OS=win32
fi
;;
macOS)
export FIBJS_OS=darwin
Expand All @@ -43,10 +59,14 @@ esac
echo "::set-output name=FIBJS_OS::$FIBJS_OS";

case "${ARCH}" in
i386)
export FIBJS_ARCH=x86
i386|ia32|x86)
if [[ "$lower_than_0_37_0" == "true" ]]; then
export FIBJS_ARCH=x86
else
export FIBJS_ARCH=ia32
fi
;;
amd64)
amd64|x64)
export FIBJS_ARCH=x64
;;
*)
Expand Down
18 changes: 0 additions & 18 deletions appveyor.yml

This file was deleted.

12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,24 @@
"ci-sqlite": "npm run build && npm run test"
},
"ci": {
"type": "actions, appveyor",
"type": "actions",
"version": [
"0.33.0",
"0.34.0",
"0.35.0"
"0.35.0",
"0.36.0",
"0.37.0"
]
},
"dependencies": {
"fib-jws": "^0.1.1",
"fib-jws": "^0.4.1",
"fib-kv": "^1.3.1"
},
"devDependencies": {
"@fibjs/ci": "^2.6.0",
"@fibjs/ci": "^2.7.1",
"@fibjs/types": "^0.35.0",
"cross-env": "^5.2.0",
"fib-pool": "^1.6.0",
"fib-typify": "^0.11.4"
"fib-typify": "^0.11.6"
}
}
17 changes: 14 additions & 3 deletions src/jwt.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
const jws = require('fib-jws');

function inputIsBuffer (bufOrString: string | Class_Buffer): bufOrString is Class_Buffer {
return Buffer.isBuffer(bufOrString);
}

export function getToken (jwt_algo: string) {
return (obj: FibSessionNS.Object, key: string) => {
return (obj: FibSessionNS.Object, key: string | Class_Buffer) => {
if (!inputIsBuffer(key))
key = new Buffer(key, 'hex')
/**
* jws.sign
* header={ alg: 'HS256' }
Expand All @@ -13,8 +19,10 @@ export function getToken (jwt_algo: string) {
}

export function setTokenCookie (jwt_algo: string, cookie_name: string) {
return (r: FibSessionNS.HttpRequest, obj: FibSessionNS.Object, key: string) => {
return (r: FibSessionNS.HttpRequest, obj: FibSessionNS.Object, key: string | Class_Buffer) => {
r.session = obj;
if (!inputIsBuffer(key))
key = new Buffer(key, 'hex')
r.sessionid = jws.sign({alg: jwt_algo}, obj, key);

r.response.addCookie({
Expand All @@ -26,7 +34,10 @@ export function setTokenCookie (jwt_algo: string, cookie_name: string) {
};
}

export function getPayload (text: string, key: string, algo: string) {
export function getPayload (text: string, key: string | Class_Buffer, algo: string) {
if (!inputIsBuffer(key))
key = new Buffer(key, 'hex')

if (jws.verify(text, key, algo)) {
var dc = jws.decode(text);
if (dc && dc.payload) {
Expand Down
4 changes: 3 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ function session_test(description, opts, test_opts, _before, _after) {
session.cookie_filter,
{
'^/user$': (r) => r.session && (r.session.username = r.query.username),
'^/get$': (r) => r.response.write(r.session.username),
'^/get$': (r) => {
r.response.write(r.session.username || '')
},
'^/del$': (r) => delete r.session.username,
'^/remove$': (r) => session.remove(r.sessionid),
'^/set$': (r) => {
Expand Down

0 comments on commit aaef3b1

Please sign in to comment.