2016-02-16 18:58:29 -05:00
|
|
|
config = require '../../server_config'
|
|
|
|
request = require 'request'
|
|
|
|
User = require '../users/User'
|
|
|
|
http = require 'http'
|
|
|
|
|
|
|
|
authstr = new Buffer("#{config.picoCTF_auth.username}:#{config.picoCTF_auth.password}").toString 'base64'
|
|
|
|
|
|
|
|
papi = (url, req, cb) ->
|
2016-02-17 12:53:45 -05:00
|
|
|
request
|
2016-02-16 18:58:29 -05:00
|
|
|
url: "#{config.picoCTF_api_url}#{url}"
|
2016-03-03 18:46:34 -05:00
|
|
|
jar: false
|
2016-02-17 12:53:45 -05:00
|
|
|
headers:
|
2016-02-16 18:58:29 -05:00
|
|
|
Cookie: "flask=#{req.cookies.flask}"
|
|
|
|
Authorization: 'Basic ' + authstr
|
|
|
|
, cb
|
|
|
|
|
|
|
|
class PicoStrategy
|
|
|
|
constructor: () ->
|
|
|
|
@name = 'local'
|
|
|
|
|
|
|
|
authenticate: (req) ->
|
|
|
|
papi "/team", req, (err, rr, body) =>
|
|
|
|
return @fail err if err
|
|
|
|
response = JSON.parse(body)
|
|
|
|
return @fail response.message if response.status is 0
|
|
|
|
|
|
|
|
data = response.data
|
|
|
|
tid = data.tid
|
|
|
|
fakeEmail = "#{tid}@coco.team"
|
|
|
|
|
|
|
|
User.findOne(emailLower: fakeEmail).exec (err, user) =>
|
|
|
|
return @success user if user
|
|
|
|
|
|
|
|
user = new User
|
|
|
|
anonymous: false
|
|
|
|
name: data.team_name
|
|
|
|
email: fakeEmail
|
|
|
|
emailLower: fakeEmail
|
2016-02-17 12:53:45 -05:00
|
|
|
aceConfig: {language: 'javascript'}
|
2016-02-16 18:58:29 -05:00
|
|
|
user.set 'testGroupNumber', Math.floor(Math.random() * 256) # also in app/core/auth
|
|
|
|
user.save (err) =>
|
|
|
|
console.log "New user created!", user
|
|
|
|
@success user
|
|
|
|
|
|
|
|
|
|
|
|
init = (app) ->
|
|
|
|
app.get '/picoctf/problems', (req, res) ->
|
|
|
|
papi "/problems/unlocked", req, (err, rr, body) ->
|
|
|
|
res.json JSON.parse(body).data
|
|
|
|
|
|
|
|
app.get '/picoctf/problems/all', (req, res) ->
|
|
|
|
papi "/problems/all", req, (err, rr, body) ->
|
|
|
|
res.json JSON.parse(body).data
|
|
|
|
|
|
|
|
app.post '/picoctf/submit', (req, res) ->
|
2016-02-17 12:53:45 -05:00
|
|
|
request
|
2016-02-16 18:58:29 -05:00
|
|
|
url: "#{config.picoCTF_APIURL}/submit"
|
|
|
|
headers:
|
|
|
|
Cookie: "flask=#{req.cookies.flask}"
|
|
|
|
Authorization: 'Basic ' + authstr
|
|
|
|
, (err, rr, body) ->
|
|
|
|
res.json JSON.parse(body)
|
|
|
|
|
2016-02-17 12:53:45 -05:00
|
|
|
module.exports =
|
2016-02-16 18:58:29 -05:00
|
|
|
PicoStrategy: PicoStrategy
|
2016-02-17 12:53:45 -05:00
|
|
|
init: init
|