-
Notifications
You must be signed in to change notification settings - Fork 6
Add device management with API key authentication #579
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| module Api | ||
| module V1 | ||
| class BaseController < ActionController::API | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| module Api | ||
| module V1 | ||
| module Beacons | ||
| class BaseController < Api::V1::BaseController | ||
| include Api::BeaconAuthentication | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| module Api | ||
| module V1 | ||
| module Beacons | ||
| class StatusesController < Beacons::BaseController | ||
| def show | ||
| render json: { | ||
| status: "ok", | ||
| beacon: { | ||
| id: Current.beacon.id, | ||
| name: Current.beacon.name, | ||
| }, | ||
| } | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| module Api | ||
| module BeaconAuthentication | ||
| extend ActiveSupport::Concern | ||
|
|
||
| included do | ||
| before_action :authenticate_beacon! | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def authenticate_beacon! | ||
| token = extract_bearer_token | ||
| return render_unauthorized("Missing authorization header") if token.blank? | ||
|
|
||
| digest = OpenSSL::Digest::SHA256.hexdigest(token) | ||
| beacon = Beacon.find_by(api_key_digest: digest) | ||
|
|
||
| return render_unauthorized("Invalid API key") if beacon.nil? | ||
| return render_unauthorized("API key has been revoked") if beacon.revoked? | ||
|
|
||
| Current.beacon = beacon | ||
| end | ||
|
|
||
| def extract_bearer_token | ||
| header = request.headers["Authorization"] | ||
| return nil if header.blank? | ||
|
|
||
| header[/\ABearer\s+(.+)\z/, 1] | ||
| end | ||
|
|
||
| def render_unauthorized(message) | ||
| render json: { error: message }, status: :unauthorized | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # == Schema Information | ||
| # | ||
| # Table name: beacons | ||
| # Database name: primary | ||
| # | ||
| # id :bigint not null, primary key | ||
| # api_key_digest :string not null | ||
| # api_key_prefix :string not null | ||
| # name :string not null | ||
| # revoked_at :datetime | ||
| # created_at :datetime not null | ||
| # updated_at :datetime not null | ||
| # language_id :bigint not null | ||
| # region_id :bigint not null | ||
| # | ||
| # Indexes | ||
| # | ||
| # index_beacons_on_api_key_digest (api_key_digest) UNIQUE | ||
| # index_beacons_on_language_id (language_id) | ||
| # index_beacons_on_region_id (region_id) | ||
| # | ||
| # Foreign Keys | ||
| # | ||
| # fk_rails_... (language_id => languages.id) | ||
| # fk_rails_... (region_id => regions.id) | ||
| # | ||
| class Beacon < ApplicationRecord | ||
| belongs_to :language | ||
| belongs_to :region | ||
|
|
||
| has_many :beacon_providers, dependent: :destroy | ||
| has_many :providers, through: :beacon_providers | ||
|
|
||
| has_many :beacon_topics, dependent: :destroy | ||
| has_many :topics, through: :beacon_topics | ||
|
|
||
| validates :name, presence: true | ||
| validates :api_key_digest, presence: true, uniqueness: true | ||
| validates :api_key_prefix, presence: true | ||
|
|
||
| scope :active, -> { where(revoked_at: nil) } | ||
| scope :revoked, -> { where.not(revoked_at: nil) } | ||
|
|
||
| def revoke! | ||
| update!(revoked_at: Time.current) | ||
| end | ||
|
|
||
| def revoked? | ||
| revoked_at.present? | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # == Schema Information | ||
| # | ||
| # Table name: beacon_providers | ||
| # Database name: primary | ||
| # | ||
| # id :bigint not null, primary key | ||
| # created_at :datetime not null | ||
| # updated_at :datetime not null | ||
| # beacon_id :bigint not null | ||
| # provider_id :bigint not null | ||
| # | ||
| # Indexes | ||
| # | ||
| # index_beacon_providers_on_beacon_id (beacon_id) | ||
| # index_beacon_providers_on_beacon_id_and_provider_id (beacon_id,provider_id) UNIQUE | ||
| # index_beacon_providers_on_provider_id (provider_id) | ||
| # | ||
| # Foreign Keys | ||
| # | ||
| # fk_rails_... (beacon_id => beacons.id) | ||
| # fk_rails_... (provider_id => providers.id) | ||
| # | ||
| class BeaconProvider < ApplicationRecord | ||
| belongs_to :beacon | ||
| belongs_to :provider | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # == Schema Information | ||
| # | ||
| # Table name: beacon_topics | ||
| # Database name: primary | ||
| # | ||
| # id :bigint not null, primary key | ||
| # created_at :datetime not null | ||
| # updated_at :datetime not null | ||
| # beacon_id :bigint not null | ||
| # topic_id :bigint not null | ||
| # | ||
| # Indexes | ||
| # | ||
| # index_beacon_topics_on_beacon_id (beacon_id) | ||
| # index_beacon_topics_on_beacon_id_and_topic_id (beacon_id,topic_id) UNIQUE | ||
| # index_beacon_topics_on_topic_id (topic_id) | ||
| # | ||
| # Foreign Keys | ||
| # | ||
| # fk_rails_... (beacon_id => beacons.id) | ||
| # fk_rails_... (topic_id => topics.id) | ||
| # | ||
| class BeaconTopic < ApplicationRecord | ||
| belongs_to :beacon | ||
| belongs_to :topic | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| class Current < ActiveSupport::CurrentAttributes | ||
| attribute :session | ||
| attribute :beacon | ||
| delegate :user, to: :session, allow_nil: true | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| module Beacons | ||
| class ApiKeyGenerator | ||
| PREFIX = "sk_live_" | ||
|
|
||
| def call | ||
| raw_key = "#{PREFIX}#{SecureRandom.hex(16)}" | ||
| digest = OpenSSL::Digest::SHA256.hexdigest(raw_key) | ||
| prefix = raw_key.delete_prefix(PREFIX)[0, 8] | ||
|
|
||
| Result.new(raw_key: raw_key, digest: digest, prefix: prefix) | ||
| end | ||
|
|
||
| Result = Data.define(:raw_key, :digest, :prefix) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| module Beacons | ||
| class Creator | ||
| attr_reader :key_generator | ||
|
|
||
| def initialize(key_generator: ApiKeyGenerator.new) | ||
| @key_generator = key_generator | ||
| end | ||
|
|
||
| def call(params) | ||
| key_result = key_generator.call | ||
|
|
||
| beacon = Beacon.new( | ||
| **params, | ||
| api_key_digest: key_result.digest, | ||
| api_key_prefix: key_result.prefix, | ||
| ) | ||
|
|
||
| if beacon.save | ||
| [ true, beacon, key_result.raw_key ] | ||
| else | ||
| [ false, beacon, nil ] | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| module Beacons | ||
| class KeyRegenerator | ||
| attr_reader :key_generator | ||
|
|
||
| def initialize(key_generator: ApiKeyGenerator.new) | ||
| @key_generator = key_generator | ||
| end | ||
|
|
||
| def call(beacon) | ||
| key_result = key_generator.call | ||
|
|
||
| beacon.update!( | ||
| api_key_digest: key_result.digest, | ||
| api_key_prefix: key_result.prefix, | ||
| revoked_at: nil, | ||
| ) | ||
|
|
||
| [ beacon, key_result.raw_key ] | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may be redundant, not sure