summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDouwe Maan <douwe@gitlab.com>2018-04-30 08:52:55 +0000
committerDouwe Maan <douwe@gitlab.com>2018-04-30 08:52:55 +0000
commitfa7b98d508a047f258b4f72d9063ec080c21cbcb (patch)
tree763b52eedbf03005d59f7e2da5e84b177483a9d2 /lib
parent7160be148e759f89d6deb3e733f536822f0dbcd5 (diff)
parent772b876a939d45530aad9f4134db1cd3232985f8 (diff)
downloadgitlab-ce-fa7b98d508a047f258b4f72d9063ec080c21cbcb.tar.gz
Merge branch 'add-jwt-strategy-to-gitlab-suite' into 'master'
Ports omniauth-jwt gem onto GitLab OmniAuth Strategies suite See merge request gitlab-org/gitlab-ce!18580
Diffstat (limited to 'lib')
-rw-r--r--lib/omni_auth/strategies/jwt.rb62
1 files changed, 62 insertions, 0 deletions
diff --git a/lib/omni_auth/strategies/jwt.rb b/lib/omni_auth/strategies/jwt.rb
new file mode 100644
index 00000000000..2349b2a28aa
--- /dev/null
+++ b/lib/omni_auth/strategies/jwt.rb
@@ -0,0 +1,62 @@
+require 'omniauth'
+require 'jwt'
+
+module OmniAuth
+ module Strategies
+ class JWT
+ ClaimInvalid = Class.new(StandardError)
+
+ include OmniAuth::Strategy
+
+ args [:secret]
+
+ option :secret, nil
+ option :algorithm, 'HS256'
+ option :uid_claim, 'email'
+ option :required_claims, %w(name email)
+ option :info_map, { name: "name", email: "email" }
+ option :auth_url, nil
+ option :valid_within, nil
+
+ uid { decoded[options.uid_claim] }
+
+ extra do
+ { raw_info: decoded }
+ end
+
+ info do
+ options.info_map.each_with_object({}) do |(k, v), h|
+ h[k.to_s] = decoded[v.to_s]
+ end
+ end
+
+ def request_phase
+ redirect options.auth_url
+ end
+
+ def decoded
+ @decoded ||= ::JWT.decode(request.params['jwt'], options.secret, options.algorithm).first
+
+ (options.required_claims || []).each do |field|
+ raise ClaimInvalid, "Missing required '#{field}' claim" unless @decoded.key?(field.to_s)
+ end
+
+ raise ClaimInvalid, "Missing required 'iat' claim" if options.valid_within && !@decoded["iat"]
+
+ if options.valid_within && (Time.now.to_i - @decoded["iat"]).abs > options.valid_within
+ raise ClaimInvalid, "'iat' timestamp claim is too skewed from present"
+ end
+
+ @decoded
+ end
+
+ def callback_phase
+ super
+ rescue ClaimInvalid => e
+ fail! :claim_invalid, e
+ end
+ end
+
+ class Jwt < JWT; end
+ end
+end