darjeelink 0.11.10 → 0.13.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0bb8e036f92bfb2145607b781537440e1dab86664c300e8313881e4d73f07940
4
- data.tar.gz: 7b8653822b7c206acc6ce03b4d0ccfad2d7ae7732b08ee597c458440ec4ba255
3
+ metadata.gz: 554db4290be45099082761d6ddac6c744af2f29e05bdca693612ce845e99c08b
4
+ data.tar.gz: 643e1a58e853742d681eadcac76eaa067d4f1de77fa30d01f38a4a30446c6f5d
5
5
  SHA512:
6
- metadata.gz: 8c64837b6368ac6f72fab25cd3f17bc0f794e74a4bd60cfe9f323dc2785624a82fc107472702ca8b6898e423c64d9ede7f3c7e5ef2a667faf07fe5ae3494b12d
7
- data.tar.gz: 541fa60fc561f4de4e476673884d9571bc8ccffaecee9b8b73b7696495ec50881da0b2807c5e0b07a32507020b62273c2141b4a84168cf0a75ff30071051f1f8
6
+ metadata.gz: dca0c51ec2669ce28986041e07893154f2639b2a57e50b147237a488f606d72ad22e8d06b6349d4a040f212b3a827f6e295eb44a63ccb67a1de833556a14ff98
7
+ data.tar.gz: 0714d875f7c7a037ea538715df0e65710d145d8600943bfcd9cf0ed24dac1ad39acccf414e13e26c9ef180d9212a9762d13e37cf5a0fb0296d12a68ae8857da9
data/README.md CHANGED
@@ -19,6 +19,48 @@ There is a UTM generator, where you can provide:
19
19
  - a campaign identifier
20
20
  And you can get a link with UTM params all filled in, and shortern it with one click.
21
21
 
22
+ ## API
23
+ There is an API available to create short links.
24
+ To create a short link via the API:
25
+
26
+ First, create an api token `Darjeelink::ApiToken.create!(username: <username>, active: true)`.
27
+ Then grab the token.
28
+
29
+ Next, make a request
30
+ ```
31
+ POST /api
32
+ Authorization => Token token=<username>:<token>
33
+ {
34
+ short_link: {
35
+ url: 'https://www.example.com',
36
+ shortened_path: 'xmpl' (optional)
37
+ }
38
+ }
39
+ ```
40
+ `url` is the absolute URI that you wish to shorten
41
+ `shortened_path` is the path that you will visit to get redirected to your original link. It is optional. If it is not provided one will be generated automatically
42
+
43
+ If successful you will get a response like:
44
+ ```
45
+ STATUS 201
46
+ {
47
+ short_link: <shortened_url>
48
+ }
49
+ ```
50
+
51
+ If unsuccessful you will get a response like
52
+ ```
53
+ STATUS 400
54
+ {
55
+ error: "information on what went wrong"
56
+ }
57
+ ```
58
+
59
+ If authorization failed you will get a response like
60
+ ```
61
+ STATUS 401
62
+ {}
63
+ ```
22
64
  ## Installation
23
65
  ### Gemfile
24
66
  Add these lines to your app's Gemfile
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Darjeelink
4
+ class ApiController < Darjeelink::ApplicationController
5
+ skip_before_action :check_ip_whitelist
6
+ skip_before_action :authenticate
7
+
8
+ before_action :authenticate_token
9
+
10
+ def create
11
+ short_link = Darjeelink::ShortLink.create!(short_link_params)
12
+
13
+ render(
14
+ json: { short_link: "#{Darjeelink.domain}/#{short_link.shortened_path}" },
15
+ status: :created
16
+ )
17
+ rescue ActiveRecord::RecordNotUnique
18
+ render(
19
+ json: { error: "#{short_link_params[:shortened_path]} already used! Choose a different custom path" },
20
+ status: :bad_request
21
+ )
22
+ rescue ActiveRecord::RecordInvalid => e
23
+ render(
24
+ json: { error: e.message.to_s },
25
+ status: :bad_request
26
+ )
27
+ rescue ActionController::ParameterMissing
28
+ render(
29
+ json: { error: 'Missing required params' },
30
+ status: :bad_request
31
+ )
32
+ end
33
+
34
+ private
35
+
36
+ def short_link_params
37
+ params.require(:short_link).permit(:url, :shortened_path)
38
+ end
39
+
40
+ def authenticate_token
41
+ # The Authorization header must be supplied in the following format:
42
+ # "Authorization" => "Token token=#{username}:#{token}"
43
+ authenticate_or_request_with_http_token do |username_token, _options|
44
+ # Perform token comparison; avoid timing attacks and length leaks
45
+ # See: https://thisdata.com/blog/timing-attacks-against-string-comparison/
46
+ return head(:unauthorized) unless valid_authorization_token?(username_token)
47
+
48
+ return true
49
+ end
50
+ end
51
+
52
+ def valid_authorization_token?(username_token)
53
+ username, token = username_token.split ':'
54
+
55
+ stored_token = ApiToken.find_by(username: username, active: true)&.token
56
+ return false if stored_token.nil?
57
+
58
+ ActiveSupport::SecurityUtils.secure_compare(
59
+ token,
60
+ stored_token
61
+ )
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Darjeelink
4
+ class ApiToken < ApplicationRecord
5
+ validates :username, uniqueness: true
6
+ validates :token, length: { minimum: 32 }, uniqueness: true
7
+
8
+ before_validation :generate_token
9
+
10
+ def generate_token
11
+ self.token = SecureRandom.uuid if token.blank?
12
+ end
13
+ end
14
+ end
@@ -14,6 +14,8 @@ Darjeelink.configure do |config|
14
14
  'sms-blast': 'SMS Blast',
15
15
  'google-advert': 'Google advert',
16
16
  'instagram-advert': 'Instagram Advert',
17
+ 'instagram-post': 'Instagram Post',
18
+ 'instagram-story': 'Instagram Story',
17
19
  chatbot: 'Chatbot',
18
20
  template: 'Template',
19
21
  other: 'Other',
data/config/routes.rb CHANGED
@@ -5,6 +5,7 @@ Darjeelink::Engine.routes.draw do
5
5
 
6
6
  resources :short_links
7
7
  resources :tracking_links, only: :new
8
+ resources :api, only: :create
8
9
 
9
10
  # OmniAuth
10
11
  get '/auth/:provider/callback', to: 'sessions#create'
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ class CreateDarjeelinkApiTokens < ActiveRecord::Migration[6.1]
4
+ def change
5
+ create_table :darjeelink_api_tokens do |t|
6
+ t.string :token, null: false
7
+ t.string :username, null: false
8
+ t.boolean :active, null: false, default: false
9
+ t.timestamps
10
+ end
11
+
12
+ add_index(:darjeelink_api_tokens, :username, unique: true)
13
+ add_index(:darjeelink_api_tokens, :token, unique: true)
14
+ end
15
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Darjeelink
4
- VERSION = '0.11.10'
4
+ VERSION = '0.13.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: darjeelink
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.10
4
+ version: 0.13.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Hulme
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-24 00:00:00.000000000 Z
11
+ date: 2022-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: omniauth
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '5'
61
+ version: '6'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '5'
68
+ version: '6'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rebrandly
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -207,6 +207,7 @@ files:
207
207
  - app/assets/javascripts/darjeelink/sparkleh.js
208
208
  - app/assets/javascripts/darjeelink/tracking_link_generator.js
209
209
  - app/assets/stylesheets/darjeelink/application.css
210
+ - app/controllers/darjeelink/api_controller.rb
210
211
  - app/controllers/darjeelink/application_controller.rb
211
212
  - app/controllers/darjeelink/sessions_controller.rb
212
213
  - app/controllers/darjeelink/short_links_controller.rb
@@ -216,6 +217,7 @@ files:
216
217
  - app/importers/darjeelink/short_link_importer.rb
217
218
  - app/jobs/darjeelink/application_job.rb
218
219
  - app/mailers/darjeelink/application_mailer.rb
220
+ - app/models/darjeelink/api_token.rb
219
221
  - app/models/darjeelink/application_record.rb
220
222
  - app/models/darjeelink/short_link.rb
221
223
  - app/views/darjeelink/short_links/_short_link_form.erb
@@ -233,6 +235,7 @@ files:
233
235
  - db/migrate/20190304094710_case_insensitive_index_short_link_shortened_path.rb
234
236
  - db/migrate/20200505220716_rename_portkey_short_links_to_darjeelink_short_links.rb
235
237
  - db/migrate/20200505221026_rename_portkey_short_link_index.rb
238
+ - db/migrate/20210610083330_create_darjeelink_api_tokens.rb
236
239
  - lib/darjeelink.rb
237
240
  - lib/darjeelink/configuration.rb
238
241
  - lib/darjeelink/engine.rb
@@ -250,14 +253,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
250
253
  requirements:
251
254
  - - ">="
252
255
  - !ruby/object:Gem::Version
253
- version: '2.6'
256
+ version: '2.7'
254
257
  required_rubygems_version: !ruby/object:Gem::Requirement
255
258
  requirements:
256
259
  - - ">="
257
260
  - !ruby/object:Gem::Version
258
261
  version: '0'
259
262
  requirements: []
260
- rubygems_version: 3.0.8
263
+ rubygems_version: 3.1.6
261
264
  signing_key:
262
265
  specification_version: 4
263
266
  summary: URL Shortener