omniauth-tanmer 1.0.5 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +25 -3
- data/lib/omniauth-tanmer/permission.rb +85 -0
- data/lib/omniauth-tanmer/version.rb +1 -1
- data/lib/omniauth-tanmer.rb +1 -0
- data/omniauth-tanmer.gemspec +2 -0
- metadata +32 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e53731b2c179028d8129aa617e8a605f503eebfd
|
4
|
+
data.tar.gz: 8518e217257f134d55c75a5e7d742b71ff46937f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b02809a68b4bf6015f92ef948ae2eb7e4e5fd47d9d6a77a0dc2070e19562fc5d25e1f5edd3046770634012451469d1a7c8d8505258606efc18f450354b981248
|
7
|
+
data.tar.gz: a7da19d88724751dc5cd578945e1b291f8b3f7f6a380b1943f0126557ab42f259b982b035eeea2f4ed91a5d3f02711559eebaf55a2f804049a51013386f9cf60
|
data/README.md
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
This is the OAuth2 strategy for authenticating to your Tanmer service.
|
4
4
|
|
5
5
|
## Requirements
|
6
|
-
|
6
|
+
|
7
7
|
## Installation
|
8
8
|
|
9
9
|
Add this line to your application's Gemfile:
|
@@ -20,10 +20,32 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
|
24
|
-
|
23
|
+
Put below code to `config/application.rb`:
|
24
|
+
|
25
|
+
config.middleware.use OmniAuth::Builder do
|
26
|
+
provider :tanmer, ENV['OAUTH_TANMER_KEY'], ENV['OAUTH_TANMER_SECRET'],
|
27
|
+
scope: 'public',
|
28
|
+
client_options: { site: ENV['OAUTH_TANMER_SITE'] }
|
25
29
|
end
|
26
30
|
|
31
|
+
## Features
|
32
|
+
|
33
|
+
Sync permissions:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
current_permissions = [
|
37
|
+
{ name: '查看', group_name: '会员', subject_class: 'Member', subject_id: nil, action: 'show', description: '' },
|
38
|
+
{ name: '创建', group_name: '会员', subject_class: 'Member', subject_id: nil, action: 'create', description: '' },
|
39
|
+
{ name: '修改', group_name: '会员', subject_class: 'Member', subject_id: nil, action: 'update', description: '' },
|
40
|
+
{ name: '删除', group_name: '会员', subject_class: 'Member', subject_id: nil, action: 'destroy', description: '' },
|
41
|
+
]
|
42
|
+
|
43
|
+
client = Omniauth::Tanmer::Permission.new(ENV['OAUTH_TANMER_HOST'], ENV['OAUTH_TANMER_KEY'], ENV['OAUTH_TANMER_SECRET'])
|
44
|
+
client.sync(current_permissions)
|
45
|
+
```
|
46
|
+
|
47
|
+
This will sync permission definitions between local project and SSO.
|
48
|
+
|
27
49
|
## Contributing
|
28
50
|
|
29
51
|
1. Fork it
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require "faraday"
|
2
|
+
require "jwt"
|
3
|
+
module Omniauth
|
4
|
+
module Tanmer
|
5
|
+
class Permission
|
6
|
+
attr_reader :app_id, :app_secret, :conn
|
7
|
+
|
8
|
+
def initialize(oauth_host, app_id, app_secret)
|
9
|
+
@app_id = app_id
|
10
|
+
@app_secret = app_secret
|
11
|
+
@conn = Faraday.new(oauth_host)
|
12
|
+
end
|
13
|
+
|
14
|
+
def remote
|
15
|
+
resp = conn.get('/api/v1/permissions.json', app_id: app_id, sn: generate_sn(SecureRandom.uuid))
|
16
|
+
JSON.parse(resp.body).map(&:symbolize_keys)
|
17
|
+
end
|
18
|
+
|
19
|
+
def sync(permissions)
|
20
|
+
remote_permissions = remote
|
21
|
+
puts "have #{permissions.size} permissions defined"
|
22
|
+
puts "got #{remote_permissions.size} permissions from API"
|
23
|
+
|
24
|
+
permissions_to_create = []
|
25
|
+
permissions_to_destroy = []
|
26
|
+
permissions_to_update = []
|
27
|
+
|
28
|
+
compare_names = %i(name group_name subject_class subject_id action description)
|
29
|
+
finder = %i(subject_class subject_id action)
|
30
|
+
|
31
|
+
# create new
|
32
|
+
permissions.each do |current_perm|
|
33
|
+
unless remote_permissions.any? { |existing_perm| finder.all?{ |k| existing_perm[k] == current_perm[k] } }
|
34
|
+
# permissions.delete(current_perm)
|
35
|
+
permissions_to_create << current_perm
|
36
|
+
end
|
37
|
+
end
|
38
|
+
puts "#{permissions_to_create.size} permissions will be created"
|
39
|
+
permissions = permissions - permissions_to_create
|
40
|
+
|
41
|
+
# destroy old
|
42
|
+
remote_permissions.each do |existing_perm|
|
43
|
+
unless permissions.any? { |current_perm| finder.all?{ |k| existing_perm[k] == current_perm[k] } }
|
44
|
+
permissions_to_destroy << existing_perm
|
45
|
+
end
|
46
|
+
end
|
47
|
+
puts "#{permissions_to_destroy.size} permissions will be deleted from API"
|
48
|
+
remote_permissions = remote_permissions - permissions_to_destroy
|
49
|
+
|
50
|
+
remote_permissions.each do |existing_perm|
|
51
|
+
current_perm = permissions.find { |current_perm| finder.all?{ |k| existing_perm[k] == current_perm[k]} }
|
52
|
+
unless compare_names.all? { |k| existing_perm[k] == current_perm[k] }
|
53
|
+
permissions_to_update << [existing_perm[:id], current_perm]
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
puts "#{permissions_to_update.size} permissions will be updated"
|
58
|
+
|
59
|
+
permissions_to_destroy.each do |perm|
|
60
|
+
resp = conn.delete("/api/v1/permissions/#{perm[:id]}", app_id: app_id, sn: generate_sn(SecureRandom.uuid))
|
61
|
+
end
|
62
|
+
|
63
|
+
permissions_to_update.each do |id, perm|
|
64
|
+
resp = conn.put("/api/v1/permissions/#{id}", permission: perm, app_id: app_id, sn: generate_sn(SecureRandom.uuid))
|
65
|
+
end
|
66
|
+
|
67
|
+
permissions_to_create.each do |perm|
|
68
|
+
resp = conn.post("/api/v1/permissions", permission: perm, app_id: app_id, sn: generate_sn(SecureRandom.uuid))
|
69
|
+
data = JSON.parse(resp.body)
|
70
|
+
end
|
71
|
+
{
|
72
|
+
created: permissions_to_create,
|
73
|
+
destroyed: permissions_to_destroy,
|
74
|
+
updated: permissions_to_update
|
75
|
+
}
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
|
80
|
+
def generate_sn(data=nil)
|
81
|
+
JWT.encode({ data: data, exp: Time.now.to_i + 300 }, app_secret, 'HS256')
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
data/lib/omniauth-tanmer.rb
CHANGED
data/omniauth-tanmer.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: omniauth-tanmer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- xiaohui
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-03-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: omniauth
|
@@ -38,6 +38,34 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '1.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: jwt
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: faraday
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
41
69
|
description: This is the strategy for authenticating to your Tanmer service
|
42
70
|
email:
|
43
71
|
- xiaohui@tanmer.com
|
@@ -52,6 +80,7 @@ files:
|
|
52
80
|
- README.md
|
53
81
|
- Rakefile
|
54
82
|
- lib/omniauth-tanmer.rb
|
83
|
+
- lib/omniauth-tanmer/permission.rb
|
55
84
|
- lib/omniauth-tanmer/version.rb
|
56
85
|
- lib/omniauth/strategies/tanmer.rb
|
57
86
|
- omniauth-tanmer.gemspec
|
@@ -76,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
105
|
version: '0'
|
77
106
|
requirements: []
|
78
107
|
rubyforge_project:
|
79
|
-
rubygems_version: 2.
|
108
|
+
rubygems_version: 2.6.14
|
80
109
|
signing_key:
|
81
110
|
specification_version: 4
|
82
111
|
summary: This is the strategy for authenticating to your Tanmer service
|