This repository was archived by the owner on Mar 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.rb
145 lines (118 loc) · 4.13 KB
/
app.rb
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
#--------------------------------------------------------
# Requirements
#--------------------------------------------------------
require 'rubygems'
require 'bundler/setup'
require 'dotenv'
require 'sinatra'
require 'haml'
require 'mediawiki_api'
require 'oauth'
require 'omniauth'
require 'omniauth-mediawiki'
require 'json'
require 'jbuilder'
require 'rest_client'
require './sinatra/utils/Hash'
if settings.environment == :development
require 'debugger'
end
#--------------------------------------------------------
# Sinatra Config
#--------------------------------------------------------
set :views, './sinatra/views'
set :raise_errors, true
set :show_exceptions, true
set :dump_errors, true
#--------------------------------------------------------
# ENV Config
#--------------------------------------------------------
Dotenv.load
#--------------------------------------------------------
# Rack Middleware
#--------------------------------------------------------
use Rack::Session::Cookie, :path => '/', :expire_after => 172800, :secret => ENV['SESSION_SECRET']
# BUILD OMNIAUTH PROVIDER
use OmniAuth::Builder do
provider :mediawiki, ENV["WIKI_KEY"], ENV["WIKI_SECRET"], :client_options => {:site => 'https://en.wikipedia.org'}
end
#--------------------------------------------------------
# Routes
#--------------------------------------------------------
# SET USER AGENT
before do
headers "User-Agent" => ENV["WIKI_USER_AGENT"]
end
get '/' do
@title = "Wikiedu Wizard"
haml :login
end
get '/welcome' do
ensure_logged_in
@wikiuser = session['wiki_username']
haml :app
end
get '/begin' do
if session['session_id']
redirect to '/welcome'
else
redirect to '/auth/mediawiki'
end
end
# Use the /test endpoint for local development without OAuth login.
get '/test' do
haml :app
end
post '/publish' do
content_type :json
@wizardData = params['wikitext']
@conn = OAuth::Consumer.new(ENV["WIKI_KEY"], ENV["WIKI_SECRET"])
@access_token = OAuth::AccessToken.new(@conn, session['access_token'], session['access_token_secret'])
get_token = @access_token.get('https://en.wikipedia.org/w/api.php?action=query&meta=tokens&format=json')
token_response = JSON.parse(get_token.body)
csrf_token = token_response['query']['tokens']['csrftoken']
apiAction = 'edit'
req = @access_token.post('https://en.wikipedia.org/w/api.php', {:action => 'edit', :title => "User:#{session['wiki_username']}/#{params['course_title']}", :text => @wizardData, :format => 'json', :token => csrf_token } )
response = JSON.parse(req.body)
if response['edit']
return { :success => true, :title => response['edit']['title'], :pageid => response['edit']['pageid'] , :result => response['edit']['result']}.to_json
else
return { :success => false, :result => response }.to_json
end
end
get '/client' do
@conn = OAuth::Consumer.new(ENV["WIKI_KEY"], ENV["WIKI_SECRET"])
@access_token = OAuth::AccessToken.new(@conn, session['access_token'], session['access_token_secret'])
get_token = @access_token.get('https://en.wikipedia.org/w/api.php?action=query&meta=tokens&format=json')
token_response = JSON.parse(get_token.body)
csrf_token = token_response['query']['tokens']['csrftoken']
res = @access_token.post('https://en.wikipedia.org/w/api.php', {:action => 'query', :meta => 'userinfo', :format => 'json' } )
userdata = JSON.parse(res.body)
session['wiki_username'] = userdata['query']['userinfo']['name']
redirect to '/welcome'
end
post '/publish_test' do
return params['wikitext']
end
get '/output' do
ensure_logged_in
@bodyClass = 'output'
haml :output
end
# MEDIAWIKI API OAUTH CALLBACK
get '/auth/:provider/callback' do
@title = 'Wikiedu Wizard - OAuth'
@auth = request.env['omniauth.auth']
@access_token = request.env["omniauth.auth"]["extra"]["access_token"]
session['access_token'] = @access_token.token
session['access_token_secret'] = @access_token.secret
redirect to '/client'
end
#--------------------------------------------------------
# Helpers
#--------------------------------------------------------
def ensure_logged_in
unless session['wiki_username'] && session['access_token']
redirect to '/'
end
end