Skip to content

Commit

Permalink
Add RBS for [email protected]
Browse files Browse the repository at this point in the history
  • Loading branch information
autopp committed Oct 16, 2022
1 parent 839d669 commit 20596f5
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,6 @@
[submodule "gems/moji/1.6/_src"]
path = gems/moji/1.6/_src
url = https://github.com/gimite/moji.git
[submodule "gems/jwt/2.5/_src"]
path = gems/jwt/2.5/_src
url = https://github.com/jwt/ruby-jwt.git
19 changes: 19 additions & 0 deletions gems/jwt/2.5/_scripts/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash

# Exit command with non-zero status code, Output logs of every command executed, Treat unset variables as an error when substituting.
set -eou pipefail
# Internal Field Separator - Linux shell variable
IFS=$'\n\t'
# Print shell input lines
set -v

# Set RBS_DIR variable to change directory to execute type checks using `steep check`
RBS_DIR=$(cd $(dirname $0)/..; pwd)
# Set REPO_DIR variable to validate RBS files added to the corresponding folder
REPO_DIR=$(cd $(dirname $0)/../../..; pwd)
# Validate RBS files, using the bundler environment present
bundle exec rbs --repo $REPO_DIR -r jwt:2.5 validate --silent

cd ${RBS_DIR}/_test
# Run type checks
bundle exec steep check
1 change: 1 addition & 0 deletions gems/jwt/2.5/_src
Submodule _src added at b4f9f1
11 changes: 11 additions & 0 deletions gems/jwt/2.5/_test/Steepfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
D = Steep::Diagnostic

target :test do
check "."
signature '.'

repo_path "../../../"
library "jwt"

configure_code_diagnostics(D::Ruby.all_error)
end
42 changes: 42 additions & 0 deletions gems/jwt/2.5/_test/test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require "jwt"

payload = { data: 'test' }
token = JWT.encode(payload, nil, 'none')
decoded, header = JWT.decode(token, nil, false)

hmac_secret = 'my$ecretK3y'
token = JWT.encode(payload, hmac_secret, 'HS256')
decoded, header = JWT.decode(token, hmac_secret, true, algorithm: 'HS256')

# Custom algorithm
class CustomAlgorithm
def alg
'custom'
end

def valid_alg?(alg_to_validate)
true
end

def sign(data:, signing_key:)
'sig'
end

def verify(data:, signature:, verification_key:)
true
end
end

alg = CustomAlgorithm.new

begin
token = ::JWT.encode(payload, 'secret', alg)
rescue JWT::EncodeError => e
puts e.backtrace
end

begin
decoded, header = ::JWT.decode(token, 'secret', true, algorithm: alg)
rescue JWT::DecodeError, JWT::VerificationError, JWT::ExpiredSignature, JWT::IncorrectAlgorithm, JWT::ImmatureSignature, JWT::InvalidIssuerError, JWT::InvalidIatError, JWT::InvalidAudError, JWT::InvalidSubError, JWT::InvalidJtiError, JWT::InvalidPayload, JWT::MissingRequiredClaim, JWT::JWKError => e
puts e.backtrace
end
6 changes: 6 additions & 0 deletions gems/jwt/2.5/_test/test.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class CustomAlgorithm
def alg: () -> ::String
def sign: (data: ::String, signing_key: untyped) -> ::String
def valid_alg?: (String alg_to_validate) -> bool
def verify: (data: untyped, signature: ::String, verification_key: untyped) -> bool
end
48 changes: 48 additions & 0 deletions gems/jwt/2.5/jwt.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module JWT
interface _Algo
def alg: () -> ::String
def sign: (data: ::String, signing_key: untyped) -> ::String
def valid_alg?: (String alg_to_validate) -> bool
def verify: (data: untyped, signature: ::String, verification_key: untyped) -> bool
end

def self?.encode: (untyped payload, untyped key, ::String | _Algo algorithm, ?::Hash[::String | ::Symbol, untyped] header_fields) -> ::String

def self?.decode: (String jwt, ?untyped? key, ?bool verify, ?::Hash[Symbol, untyped] options) ?{ (Hash[::String, untyped] header, ?untyped payload) -> untyped } -> [untyped, Hash[::String, untyped]]


class EncodeError < StandardError
end
class DecodeError < StandardError
end
class RequiredDependencyError < StandardError
end

class VerificationError < DecodeError
end
class ExpiredSignature < DecodeError
end
class IncorrectAlgorithm < DecodeError
end
class ImmatureSignature < DecodeError
end
class InvalidIssuerError < DecodeError
end
class UnsupportedEcdsaCurve < IncorrectAlgorithm
end
class InvalidIatError < DecodeError
end
class InvalidAudError < DecodeError
end
class InvalidSubError < DecodeError
end
class InvalidJtiError < DecodeError
end
class InvalidPayload < DecodeError
end
class MissingRequiredClaim < DecodeError
end

class JWKError < DecodeError
end
end

0 comments on commit 20596f5

Please sign in to comment.