codecombat/server/lib/picoctf.coffee

66 lines
1.8 KiB
CoffeeScript
Raw Normal View History

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) ->
request
2016-02-16 18:58:29 -05:00
url: "#{config.picoCTF_api_url}#{url}"
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
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) ->
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)
module.exports =
2016-02-16 18:58:29 -05:00
PicoStrategy: PicoStrategy
init: init