forked from Morendil/referentiel.institut-agile.fr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.ru
156 lines (132 loc) · 3.74 KB
/
config.ru
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
require 'sinatra'
require 'tilt'
require 'json'
require 'haml'
require 'mustache'
require 'sinatra/mustache'
require 'openid_connect'
require 'cgi'
require './lib/roadmap'
require './lib/helpers'
Tilt.register "ham", Tilt[:haml]
r = Roadmap.new("src")
use Rack::Session::Pool, :expire_after => 60 * 60
set :session_secret, ENV['session_secret']
helpers do
def before_render template, data={}
Mustache.template_file = template
m = Mustache.new
yield m if block_given?
haml m.render data
end
end
before do
expires 500, :public, :must_revalidate
end
before '/' do
request.path_info = '/index.html'
end
## Login workflow
get '/status' do
cache_control :no_cache
@profile = session[:profile]
@profile ? (haml :logged, :layout=>false) : (haml :notlogged, :layout=>false)
end
get '/logout' do
session.delete :profile
redirect 'https://fcp.integ01.dev-franceconnect.fr/api/v1/logout'
end
get '/login' do
session[:state] = Random::DEFAULT.rand.to_s
client = OpenIDConnect::Client.new(
identifier: ENV['FranceConnect_Key'],
secret: ENV['FranceConnect_Secret'],
redirect_uri: 'http://institut-agile.fr/oidc_callback',
host: 'fcp.integ01.dev-franceconnect.fr',
authorization_endpoint: '/api/v1/authorize',
token_endpoint: '/api/v1/token',
userinfo_endpoint: '/api/v1/userinfo'
)
authorization_uri = client.authorization_uri(
state: session[:state],
scope: [:profile, :email]
)
puts authorization_uri
redirect authorization_uri
end
get '/oidc_callback' do
begin
client = OpenIDConnect::Client.new(
identifier: ENV['FranceConnect_Key'],
secret: ENV['FranceConnect_Secret'],
redirect_uri: 'http://institut-agile.fr/oidc_callback',
host: 'fcp.integ01.dev-franceconnect.fr',
authorization_endpoint: '/api/v1/authorize',
token_endpoint: '/api/v1/token',
userinfo_endpoint: '/api/v1/userinfo'
)
client.authorization_code = params[:code]
token = client.access_token! (:acid)
access_token = OpenIDConnect::AccessToken.new(
access_token: token,
client: client
)
session[:profile] = @profile = access_token.userinfo!
rescue Exception => e
puts e
end
where = params[:backto] || "/";
puts "Logged in: #{@profile.given_name} #{@profile.family_name}" if @profile
redirect to(where)
end
## Dynamic content
get '/index_json' do
content_type 'text/json', :charset => 'utf-8'
r.all.to_json
end
get '/index_alpha.html' do
before_render "views/index.tmpl" do |m|
m[:parts] = r.all_by_alpha
m[:order] = "ordre alphabétique"
end
end
get '/index_type.html' do
before_render "views/index.tmpl" do |m|
m[:parts] = r.all_by_type
m[:order] = "type"
end
end
get '/*.html' do
src = r.find_by_id(params[:splat].first)
pass unless src
@default_layout = nil
headers["Access-Control-Allow-Origin"]="*"
page = before_render "views/practice.tmpl", src
end
get '/comments/:id' do |id|
cache_control :no_cache
@id = id
mustache :disqus, :layout => false
end
## Static content
['index','outils','ebook','inconnu'].each do |static|
get "/#{static}.html" do
haml static.intern
end
end
## Static assets
before '/assets/AgileDeAaZ.pdf' do
puts "PDF: #{@profile.first_name} #{@profile.last_name}" if @profile
end
get '/assets/*' do |file|
send_file File.join('site',request.path)
end
['/*.js','*.css'].each do |path|
get path do |file|
send_file File.join('site/assets',request.path)
end
end
not_found do
"<h1>Cette page n'existe pas</h1>"
end
run Sinatra::Application