ssk_line 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/ssk_line.rb +113 -0
  3. metadata +71 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: eee3c632759a25a502489ed1f9750e86d9c62d1086e26f8118763e2a2f9dd8e5
4
+ data.tar.gz: f9f4d6bc518b7512bac35a23a41f5aafe0269433e16007fc1768b956ce25898a
5
+ SHA512:
6
+ metadata.gz: f361a8cc9e3790401ca4667cbd1cb68b06eb740f3806fb79a4b72158444710912fd2c34830e48736bf4ad0dcd1304d85ad2db818c19c5926454c9f03ac00eed1
7
+ data.tar.gz: f7b6052d91a14ed45e55efb023f2ab43c7c484cd341a4ee66e02e39cb71c3bc23dbbf32d4d060a3396a1cbac13183480e658f79190878b7a66f1b464deea0986
data/lib/ssk_line.rb ADDED
@@ -0,0 +1,113 @@
1
+ require 'ht_req'
2
+ module SskLine
3
+
4
+ #セッションに現在のcsrfトークンを入れて、
5
+ #Lineログイン用のURLを返す関数
6
+ def self.login_url(request)
7
+ if self.env_not_exist?(request) then return false end
8
+ request.session[:line_csrf] = request.session[:_csrf_token]
9
+ 'https://access.line.me/oauth2/v2.1/authorize?response_type=code&client_id='+ENV['LINEAPI_CHANNEL_ID']+'&redirect_uri='+ENV['LINEAPI_CALLBACK_URL']+'&state='+request.session[:line_csrf]+'&scope=profile%20openid'
10
+ end
11
+
12
+ def self.status_check(request,controller,user_class)
13
+ request.flash[:danger]=''
14
+ self.env_key_check(request,'LINEAPI_CHANNEL_ID')
15
+ self.env_key_check(request,'LINEAPI_CALLBACK_URL')
16
+ self.env_key_check(request,'LINEAPI_CALLBACK_URL')
17
+ self.instance_method_check(request,user_class,:teacher)
18
+ self.instance_method_check(request,user_class,:admin)
19
+ self.instance_method_check(request,user_class,:lineid)
20
+ self.instance_method_check(request,user_class,:dname)
21
+ self.instance_method_check(request,user_class,:picture)
22
+
23
+
24
+
25
+ if request.flash[:danger].length > 0 then return false else return true end
26
+
27
+ end
28
+
29
+ def self.instance_method_check(request,user_class,key)
30
+ if user_class.new.methods.include?(key) == false then request.flash[:danger] =','+user_class.name.to_s+'に'+key.to_s+'が存在しません' end
31
+ end
32
+
33
+ def self.env_key_check(request,key)
34
+ if ENV.has_key?(key) == false then request.flash[:danger]=',環境変数'+key+'が存在しません' end
35
+ end
36
+
37
+ def self.login_process(line_profile,user_class,request)
38
+ line_profile.has_key?("pictureUrl") ? picture = line_profile["pictureUrl"] : picture = nil
39
+ #最初のユーザーは管理者にする。それ以外は初期値としては管理者にしない
40
+ user_class::count == 0 ? admin = true : admin = false
41
+ #ユーザーが存在しなければ
42
+ if user_class::where('lineid = ?',line_profile["userId"]).count == 0
43
+ #新規ユーザーとして追加
44
+ user = user_class.create(:lineid =>line_profile["userId"],:dname=>line_profile["displayName"],:picture=>picture,:admin=>admin,:teacher=>false)
45
+ else
46
+ user=user_class.find_by(:lineid => line_profile["userId"])
47
+ user.update(:dname=>line_profile["displayName"],:picture=>picture)
48
+ end
49
+ request.session[:user_id]=user.id
50
+ end
51
+
52
+ def self.get_line_profile(access_token)
53
+ res = HtReq.get_json_data({
54
+ :method => 'GET',
55
+ :url => 'https://api.line.me/v2/profile',
56
+ :params =>{},
57
+ :header=>{'Authorization'=>'Bearer '+access_token}
58
+ })
59
+ if res then return res else return false end
60
+ end
61
+
62
+ def self.get_access_token(request,controller)
63
+ if request.params.has_key?(:code) == false then request.flash[:danger] = 'codeがありません' and return false end
64
+ res = HtReq.get_json_data({
65
+ :method => 'POST',
66
+ :url => 'https://api.line.me/oauth2/v2.1/token',
67
+ :params =>{
68
+ 'grant_type'=>'authorization_code',
69
+ 'code'=>request.params[:code],
70
+ 'redirect_uri'=>ENV['LINEAPI_CALLBACK_URL'],
71
+ 'client_id'=>ENV['LINEAPI_CHANNEL_ID'],
72
+ 'client_secret'=>ENV['LINEAPI_CHANNEL_SECRET']
73
+ },
74
+ :header=>{'Content-Type'=>'application/x-www-form-urlencoded'}
75
+ })
76
+ if res then return res["access_token"] else return false end
77
+ end
78
+
79
+
80
+ def self.line_login_process(request,controller,user_class)
81
+ if self.status_check(request,controller,user_class) == false then controller.redirect_to controller.root_path and return false end
82
+ if self.line_csrf_check(request) == false then controller.redirect_to controller.root_path and return false end
83
+ access_token = self.get_access_token(request,controller)
84
+ if access_token == false then controller.redirect_to controller.root_path and request.flash[:danger]='アクセストークンが取得できません。' and return false end
85
+ profile = self.get_line_profile(access_token)
86
+ if profile == false then controller.redirect_to controller.root_path and request.flash[:danger]='LINEアカウントのプロフィールが取得できません。' and return false end
87
+ self.login_process(profile,user_class,request)
88
+ return true
89
+ end
90
+
91
+ def self.line_csrf_check(request)
92
+ #ログインしていないのにstateがなければfalse
93
+ if request.params.has_key?(:state) == false then request.flash[:danger] = 'ログインしてください' and return false end
94
+ #セッションにline_csrfがなければfalse
95
+ if request.session.has_key?(:line_csrf) === false then request.flash[:danger] = 'ログインしてください' and return false end
96
+ #トークンが一致しなければfalse、一致していればセッションからline_csrfを削除
97
+ if request.params[:state] == request.session[:line_csrf] then request.session.delete(:line_csrf) and return true else request.flash[:danger]='トークンが一致しません' and return false end
98
+ end
99
+
100
+ private
101
+
102
+ def self.env_not_exist?(request)
103
+ if ENV.has_key?('LINEAPI_CHANNEL_ID') && ENV.has_key?('LINEAPI_CALLBACK_URL') && ENV.has_key?('LINEAPI_CHANNEL_SECRET')
104
+ return false
105
+ else
106
+ request.flash.now[:danger]='SskLineを使用するために必要な環境変数が設定されておりません'
107
+ return true
108
+ end
109
+ end
110
+
111
+ end
112
+
113
+
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ssk_line
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Nick Quaranto
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-12-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ht_req
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: This gem is for seishinkai line
42
+ email: satoshi@expresstutor.tokyo
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/ssk_line.rb
48
+ homepage: https://github.com/SatoshiTagomori/ssk_line
49
+ licenses:
50
+ - MIT
51
+ metadata: {}
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubygems_version: 3.2.22
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: This gem is for seishinkai line
71
+ test_files: []