Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
ApiPack is a set of helpers to Api rails
This project provides the following helpers
ruby >= 2.4
Add this line to your application's Gemfile:
gem 'api_pack', '~> 1.3.1'
And then execute:
$ bundle
Or install it yourself as:
$ gem install api_pack
Rails.application.config.to_prepare do
ApiPack.hmac_secret = 'your_secret_key'
end
ApiPack::JsonWebToken.encode({ user_id: user.id, exp: 123 })
Usage in a service authenticate user
ApiPack use gem JWT
class AuthenticateUser
def initialize(email, password)
@email = email
@password = password
end
def call
# return token valid
ApiPack::JsonWebToken.encode({ user_id: user.id }) if user
end
private
attr_accessor :email, :password
def user
user = User.find_by(email: email)
return user if user&.authenticate(password)
# raise AuthenticationError if authenticate fail
raise(ApiPack::Errors::Auth::AuthenticationError, 'Invalid credentials')
end
end
Usage in a service authorize api request
class AuthorizeApiRequest
def initialize(headers: {})
@headers = headers
end
def call
{ user: user }
end
private
attr_accessor :headers
def user
@user ||= User.find(decoded_auth_token['user_id']) if decoded_auth_token
rescue ActiveRecord::RecordNotFound => e
# raise InvalidToken if user not found
raise ApiPack::Errors::Auth::InvalidToken, ("Invalid token #{e.message}")
end
def decoded_auth_token
# decode a token valid
@decoded_auth_token ||= ApiPack::JsonWebToken.decode(http_auth_header)
end
def http_auth_header
return headers['Authorization'].split(' ').last if headers['Authorization'].present?
raise(ApiPack::Errors::Auth::MissingToken, 'Missing token')
end
end
Errors handled
ActiveRecord::RecordNotFound
ActionController::ParameterMissing
ActiveRecord::RecordInvalid
ApiPack::Errors::HandleError.new(e).call
create an ExceptionHandler concern and deal with api errors in json api format
module ExceptionHandler
extend ActiveSupport::Concern
included do
rescue_from StandardError do |e|
result = ApiPack::Errors::HandleError.new(e).call
render json: result[:body], status: result[:status]
end
end
end
ApiPack.defaut_per_page = 12
def index
# current_page mtehod default is 1
# per_page method default is 10
users = User.page(current_page).per_page(per_page)
options = pagination_meta_generator(request, users.total_pages)
render json: serializer_hash(users, :user, opt: options)
end
https://github.com/Jllima/api_pack_rails
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/api_pack.
The gem is available as open source under the terms of the MIT License.
FAQs
Unknown package
We found that api_pack demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.