cyclid-ui 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +178 -0
  3. data/README.md +20 -0
  4. data/app/cyclid_ui.rb +124 -0
  5. data/app/cyclid_ui/config.rb +64 -0
  6. data/app/cyclid_ui/controllers/auth.rb +102 -0
  7. data/app/cyclid_ui/controllers/base.rb +41 -0
  8. data/app/cyclid_ui/controllers/health.rb +96 -0
  9. data/app/cyclid_ui/controllers/organization.rb +64 -0
  10. data/app/cyclid_ui/controllers/user.rb +40 -0
  11. data/app/cyclid_ui/helpers.rb +55 -0
  12. data/app/cyclid_ui/memcache.rb +45 -0
  13. data/app/cyclid_ui/models/user.rb +91 -0
  14. data/app/cyclid_ui/templates/footer.mustache +8 -0
  15. data/app/cyclid_ui/templates/job.mustache +21 -0
  16. data/app/cyclid_ui/templates/job_info.mustache +40 -0
  17. data/app/cyclid_ui/templates/layout.mustache +102 -0
  18. data/app/cyclid_ui/templates/login.mustache +78 -0
  19. data/app/cyclid_ui/templates/organization.mustache +150 -0
  20. data/app/cyclid_ui/templates/user.mustache +97 -0
  21. data/app/cyclid_ui/views/job.rb +25 -0
  22. data/app/cyclid_ui/views/layout.rb +52 -0
  23. data/app/cyclid_ui/views/login.rb +25 -0
  24. data/app/cyclid_ui/views/organization.rb +25 -0
  25. data/app/cyclid_ui/views/user.rb +25 -0
  26. data/bin/cyclid-ui-assets +17 -0
  27. data/lib/cyclid_ui/app.rb +4 -0
  28. data/public/images/LICENSE +3 -0
  29. data/public/images/cyclid-logo-large.png +0 -0
  30. data/public/images/favicon16.png +0 -0
  31. data/public/images/favicon32.png +0 -0
  32. data/public/images/favicon48.png +0 -0
  33. data/public/images/favicon64.png +0 -0
  34. data/public/images/favicon96.png +0 -0
  35. data/public/js/api.js +34 -0
  36. data/public/js/cyclid.js +32 -0
  37. data/public/js/job.js +215 -0
  38. data/public/js/organization.js +345 -0
  39. data/public/js/user.js +145 -0
  40. data/public/vendor/bootstrap/css/bootstrap-theme.css +587 -0
  41. data/public/vendor/bootstrap/css/bootstrap-theme.css.map +1 -0
  42. data/public/vendor/bootstrap/css/bootstrap-theme.min.css +6 -0
  43. data/public/vendor/bootstrap/css/bootstrap-theme.min.css.map +1 -0
  44. data/public/vendor/bootstrap/css/bootstrap.css +6760 -0
  45. data/public/vendor/bootstrap/css/bootstrap.css.map +1 -0
  46. data/public/vendor/bootstrap/css/bootstrap.min.css +6 -0
  47. data/public/vendor/bootstrap/css/bootstrap.min.css.map +1 -0
  48. data/public/vendor/bootstrap/css/custom.css +193 -0
  49. data/public/vendor/bootstrap/fonts/glyphicons-halflings-regular.eot +0 -0
  50. data/public/vendor/bootstrap/fonts/glyphicons-halflings-regular.svg +288 -0
  51. data/public/vendor/bootstrap/fonts/glyphicons-halflings-regular.ttf +0 -0
  52. data/public/vendor/bootstrap/fonts/glyphicons-halflings-regular.woff +0 -0
  53. data/public/vendor/bootstrap/fonts/glyphicons-halflings-regular.woff2 +0 -0
  54. data/public/vendor/bootstrap/js/bootstrap.js +2363 -0
  55. data/public/vendor/bootstrap/js/bootstrap.min.js +7 -0
  56. data/public/vendor/bootstrap/js/npm.js +13 -0
  57. data/public/vendor/font-awesome/HELP-US-OUT.txt +7 -0
  58. data/public/vendor/font-awesome/css/font-awesome.css +2199 -0
  59. data/public/vendor/font-awesome/css/font-awesome.min.css +4 -0
  60. data/public/vendor/font-awesome/fonts/FontAwesome.otf +0 -0
  61. data/public/vendor/font-awesome/fonts/fontawesome-webfont.eot +0 -0
  62. data/public/vendor/font-awesome/fonts/fontawesome-webfont.svg +685 -0
  63. data/public/vendor/font-awesome/fonts/fontawesome-webfont.ttf +0 -0
  64. data/public/vendor/font-awesome/fonts/fontawesome-webfont.woff +0 -0
  65. data/public/vendor/font-awesome/fonts/fontawesome-webfont.woff2 +0 -0
  66. data/public/vendor/font-awesome/less/animated.less +34 -0
  67. data/public/vendor/font-awesome/less/bordered-pulled.less +25 -0
  68. data/public/vendor/font-awesome/less/core.less +12 -0
  69. data/public/vendor/font-awesome/less/fixed-width.less +6 -0
  70. data/public/vendor/font-awesome/less/font-awesome.less +18 -0
  71. data/public/vendor/font-awesome/less/icons.less +733 -0
  72. data/public/vendor/font-awesome/less/larger.less +13 -0
  73. data/public/vendor/font-awesome/less/list.less +19 -0
  74. data/public/vendor/font-awesome/less/mixins.less +60 -0
  75. data/public/vendor/font-awesome/less/path.less +15 -0
  76. data/public/vendor/font-awesome/less/rotated-flipped.less +20 -0
  77. data/public/vendor/font-awesome/less/screen-reader.less +5 -0
  78. data/public/vendor/font-awesome/less/stacked.less +20 -0
  79. data/public/vendor/font-awesome/less/variables.less +744 -0
  80. data/public/vendor/font-awesome/scss/_animated.scss +34 -0
  81. data/public/vendor/font-awesome/scss/_bordered-pulled.scss +25 -0
  82. data/public/vendor/font-awesome/scss/_core.scss +12 -0
  83. data/public/vendor/font-awesome/scss/_fixed-width.scss +6 -0
  84. data/public/vendor/font-awesome/scss/_icons.scss +733 -0
  85. data/public/vendor/font-awesome/scss/_larger.scss +13 -0
  86. data/public/vendor/font-awesome/scss/_list.scss +19 -0
  87. data/public/vendor/font-awesome/scss/_mixins.scss +60 -0
  88. data/public/vendor/font-awesome/scss/_path.scss +15 -0
  89. data/public/vendor/font-awesome/scss/_rotated-flipped.scss +20 -0
  90. data/public/vendor/font-awesome/scss/_screen-reader.scss +5 -0
  91. data/public/vendor/font-awesome/scss/_stacked.scss +20 -0
  92. data/public/vendor/font-awesome/scss/_variables.scss +744 -0
  93. data/public/vendor/font-awesome/scss/font-awesome.scss +18 -0
  94. data/public/vendor/jquery-2.2.4.min.js +4 -0
  95. data/public/vendor/jquery.md5.js +269 -0
  96. data/public/vendor/js.cookie.js +151 -0
  97. data/public/vendor/mustache.min.js +1 -0
  98. metadata +322 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9814c8d110ccfe013e04cea146f6ff28b25ccd3c
4
+ data.tar.gz: f3501d1380185c9a772b61e3df0947031974fee2
5
+ SHA512:
6
+ metadata.gz: f1ababb60d429b122929875c5b9299c5b75af95c9e50422ecadf694ad7938bb6a8c5de4ca7a4c1ee5044daa85280fcbb56880a0f673052a243489a940215ba15
7
+ data.tar.gz: 73792917cfe866677dc6a3960da9e8547627d87294727724b0e2a30874b9531ece5c668839d4a55fcdd79ccfb010b59fa51dcf2d0fd2664a640764540fd3e0d4
data/LICENSE ADDED
@@ -0,0 +1,178 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ The Cyclid Logo and its derivatives are licensed under the Creative Commons
177
+ Attribution-NonCommercial-NoDerivatives 4.0 International License. To view a
178
+ copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/.
@@ -0,0 +1,20 @@
1
+ Cyclid-UI
2
+ =========
3
+ This is the web based user interface to the Cyclid CI server.
4
+
5
+ # Getting started
6
+ ```
7
+ $ rbenv install 2.3.0
8
+ $ bundle install --path vendor/bundle
9
+ ```
10
+ Cyclid-UI can optionally use Memcached for storing some user object data; if you choose to run a Memcached server on your local machine Cyclid-UI will connect by default on `localhost:11211`.
11
+
12
+ Cyclid-UI requires a Cyclid API server that both it and the client (I.e. the web browser) can connect too. See the documentation for the Cyclid API server for information on how to install & configure Cyclid.
13
+
14
+ You can start Cyclid-UI under Webrick with `bundle exec rake rackup` or you can run under Guard with `bundle exec rake guard`.
15
+
16
+ # Testing
17
+
18
+ RSpec tests are included. Run `bundle exec rake spec` to run the tests and generate a coverage report into the `coverage` directory. The tests do not affect any databases and external API calls are mocked.
19
+
20
+ The Cyclid-UI source code is also expected to pass Rubocop; run `bundle exec rake rubocop` to lint the code.
@@ -0,0 +1,124 @@
1
+ # frozen_string_literal: true
2
+ # Copyright 2016 Liqwyd Ltd.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ require 'require_all'
17
+ require 'logger'
18
+ require 'warden'
19
+ require 'memcached'
20
+ require 'sinatra/flash'
21
+ require 'rack/csrf'
22
+
23
+ require_rel 'cyclid_ui/config'
24
+ require_rel 'cyclid_ui/memcache'
25
+ require_rel 'cyclid_ui/helpers'
26
+
27
+ require_rel 'cyclid_ui/models'
28
+ require_rel 'cyclid_ui/controllers'
29
+
30
+ require_rel 'cyclid_ui/views'
31
+
32
+ # Namespace for all Cyclid UI related code
33
+ module Cyclid
34
+ class << self
35
+ attr_accessor :config, :logger
36
+
37
+ config_path = ENV['CYCLID_CONFIG'] || File.join(%w(/ etc cyclid config))
38
+ Cyclid.config = UI::Config.new(config_path)
39
+
40
+ begin
41
+ Cyclid.logger = if Cyclid.config.log.casecmp('stderr').zero?
42
+ Logger.new(STDERR)
43
+ else
44
+ Logger.new(Cyclid.config.log)
45
+ end
46
+ rescue StandardError => ex
47
+ abort "Failed to initialize: #{ex}"
48
+ end
49
+ end
50
+ end
51
+
52
+ module Cyclid
53
+ module UI
54
+ # Sinatra application
55
+ class App < Sinatra::Application
56
+ configure do
57
+ set sessions: true,
58
+ secure: production?,
59
+ expire_after: 31_557_600,
60
+ secret: ENV['SESSION_SECRET']
61
+ end
62
+
63
+ use Rack::Deflater
64
+ use Rack::Session::Cookie
65
+ use Rack::Csrf, raise: true,
66
+ skip: ['POST:/login',
67
+ 'POST:/unauthenticated']
68
+
69
+ helpers Helpers
70
+
71
+ register Sinatra::Flash
72
+
73
+ # Configure Warden to authenticate
74
+ use Warden::Manager do |config|
75
+ config.serialize_into_session(&:username)
76
+ config.serialize_from_session do |username|
77
+ # Animal skins & flint knives...
78
+ token = env['rack.request.cookie_hash']['cyclid.token']
79
+ Models::User.get(username: username, token: token)
80
+ end
81
+
82
+ config.scope_defaults :default,
83
+ strategies: [:session],
84
+ action: '/unauthenticated'
85
+
86
+ config.failure_app = self
87
+ end
88
+
89
+ Warden::Strategies.add(:session) do
90
+ def valid?
91
+ session.key? :username
92
+ end
93
+
94
+ def authenticate!
95
+ username = session[:username]
96
+ # There are no Helpers in Wardentown
97
+ token = request.cookies['cyclid.token']
98
+
99
+ user = Models::User.get(username: username, token: token)
100
+
101
+ user.nil? ? fail!('invalid user') : success!(user)
102
+ end
103
+ end
104
+
105
+ Warden::Manager.before_failure do |env, _opts|
106
+ env['REQUEST_METHOD'] = 'POST'
107
+ end
108
+
109
+ post '/unauthenticated' do
110
+ # Stop Warden from calling this endpoint again in an endless loop when
111
+ # it sees the 401 response
112
+ env['warden'].custom_failure!
113
+ flash[:login_error] = 'Invalid username or password'
114
+ redirect to '/login'
115
+ end
116
+
117
+ # Register the other routes
118
+ use Controllers::Auth
119
+ use Controllers::Organization
120
+ use Controllers::User
121
+ use Controllers::Health
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+ # Copyright 2016 Liqwyd Ltd.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ require 'yaml'
17
+ require 'uri'
18
+
19
+ module Cyclid
20
+ module UI
21
+ # Cyclid UI configuration
22
+ class Config
23
+ attr_reader :memcached, :log, :server_api, :client_api
24
+
25
+ def initialize(path)
26
+ # Try to load the configuration file. If it can't be loaded, we'll
27
+ # fall back to defaults
28
+ begin
29
+ config = YAML.load_file(path)
30
+ manage = config['manage'] || {}
31
+ rescue Errno::ENOENT
32
+ # Cyclid.logger wont exist, yet
33
+ STDERR.puts "Config file #{path} not found: using defaults"
34
+ manage = {}
35
+ end
36
+
37
+ @memcached = manage['memcached'] || 'localhost:11211'
38
+ @log = manage['log'] || File.join(%w(/ var log cyclid manage))
39
+
40
+ # The api setting is flexible; the URL that the server uses to connect
41
+ # to the API, and the URL that the client uses, can be diferent. This
42
+ # may happen if E.g. the UI & API are running on the same machine but
43
+ # with the ports NAT'd from the outside so that the client sees port
44
+ # 8123 but the local port is 123, or where the API is behind a load
45
+ # balancer and you may wish to avoid the round-trip out and back in.
46
+ #
47
+ # If "api" is a string then it is used for both the server & client URL
48
+ # If "api" is an array then we select the client & server URLs
49
+ # separately
50
+ api = manage['api'] || 'http://localhost:8361'
51
+ if api.is_a? String
52
+ @server_api = URI(api)
53
+ @client_api = URI(api)
54
+ elsif api.is_a? Hash
55
+ @server_api = URI(api['server'])
56
+ @client_api = URI(api['client'])
57
+ end
58
+
59
+ rescue StandardError => ex
60
+ abort "Failed to load configuration file #{path}: #{ex}"
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+ # Copyright 2016 Liqwyd Ltd.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ require 'json'
17
+ require 'cyclid/client'
18
+
19
+ module Cyclid
20
+ module UI
21
+ module Controllers
22
+ # Controller for authentication related endpoints
23
+ class Auth < Base
24
+ get '/login' do
25
+ @message = flash[:login_error]
26
+ mustache :login, layout: false
27
+ end
28
+
29
+ post '/login' do
30
+ username = params[:username]
31
+ password = params[:password]
32
+
33
+ # Use the username & password to authenticate against the API; if
34
+ # successful, it will return a JWT that can be used to authenticate
35
+ # future requests
36
+ begin
37
+ Cyclid.logger.debug "api=#{Cyclid.config.server_api}"
38
+ client = Client::Tilapia.new(auth: Client::AUTH_BASIC,
39
+ log_level: Logger::DEBUG,
40
+ server: Cyclid.config.server_api.host,
41
+ port: Cyclid.config.server_api.port,
42
+ username: username,
43
+ password: password)
44
+ token_data = client.token_get(username)
45
+ Cyclid.logger.debug "got #{token_data}"
46
+ rescue StandardError => ex
47
+ Cyclid.logger.fatal "failed to get a token: #{ex}"
48
+ halt_with_401
49
+ end
50
+
51
+ # At this point the user has authenticated successfully; get the user
52
+ # information; the User model will cache it automatically.
53
+ user = Models::User.get(username: username, password: password)
54
+ Cyclid.logger.debug "user=#{user.to_hash}"
55
+
56
+ # Store the username in the session
57
+ session[:username] = username
58
+
59
+ # Return the JWT cookie to the client as a cookie
60
+ response.set_cookie('cyclid.token',
61
+ value: token_data['token'],
62
+ expires: Time.now + 21_600_000, # +6 hours
63
+ path: '/',
64
+ http_only: false) # Must be available for AJAX
65
+
66
+ # Pick the first organization from the users membership and
67
+ # redirect; if the user doesn't belong to any organizations,
68
+ # redirect them to their user page
69
+ initial_page = if user.organizations.empty?
70
+ "/user/#{username}"
71
+ else
72
+ user.organizations.first
73
+ end
74
+
75
+ redirect to initial_page
76
+ end
77
+
78
+ # Log out:
79
+ #
80
+ # 1. Delete the cached user object from Memcached
81
+ # 2. Clear the session data
82
+ # 3. Delete the API token cookie
83
+ get '/logout' do
84
+ authenticate!
85
+
86
+ memcache = Memcache.new(server: Cyclid.config.memcached)
87
+ begin
88
+ memcache.expire(current_user.username)
89
+ rescue Memcached::ServerIsMarkedDead => ex
90
+ Cyclid.logger.fatal "cannot connect to memcached: #{ex}"
91
+ # If Memcache is down there is nothing to expire
92
+ end
93
+
94
+ warden.logout
95
+ cookies.delete 'cyclid.token'
96
+
97
+ redirect to '/login'
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end