From 9f281941f885f6fdcf878ce4c8b2cdb0f525c2a9 Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Sat, 15 Oct 2011 16:55:35 -0400 Subject: [PATCH] auth working. rename auth.coffee -> api.coffee --- api.coffee | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ auth.coffee | 81 ----------------------------------------------------- 2 files changed, 89 insertions(+), 81 deletions(-) create mode 100644 api.coffee delete mode 100644 auth.coffee diff --git a/api.coffee b/api.coffee new file mode 100644 index 0000000..2ac50c5 --- /dev/null +++ b/api.coffee @@ -0,0 +1,89 @@ +fs = require 'fs' +http = require 'http' +crypto = require 'crypto' +xml = require 'node-xml' + +# soooo annoying that setTimeout takes the ms arg last +timeout = (ms, func) -> setTimeout func, ms + +auth = {} + +md5 = (str) -> + sum = crypto.createHash 'md5' + sum.update str + return sum.digest 'hex' + +new_auth_token = (user, pass) -> + token = md5(user + md5(pass)) + auth.user = user + auth.token = token + return token + +auth_file = "#{process.env.HOME}/.libre.fm-cmus.auth" +save_auth = (user, pass, callback) -> + token = new_auth_token(user, pass) + text = JSON.stringify user: user, token: new_auth_token(user, pass) + fs.writeFile auth_file, text, 'utf8', callback + return token + +# load login credentials from settings file +load_auth = (callback) -> + if auth.user and auth.token + callback auth + return + + fs.readFile auth_file, 'utf8', (err, data) -> + if err + callback err + else + callback null, JSON.parse data + +# login and get a session key +login = (callback) -> + load_auth (err, auth) -> + return callback(err) if err? + + http.get { host: 'alpha.libre.fm', port: 80, path: "/2.0/?method=auth.getmobilesession&username=#{auth.user}&authToken=#{auth.token}"}, (res) -> + if res.statusCode != 200 + console.log "login response code: #{res.statusCode}" + callback "login response code: #{res.statusCode}" + return + + res.setEncoding 'utf8' + body = '' + res.on 'data', (chunk) -> + body += chunk + res.on 'end', -> + element = '' + content = '' + done = false + huh = -> + # FIXME switch to an xml parser that will tell me when it's done + unless done + done = true + callback "xml parser failed to do anything with login server response" + console.log "xml parser didn't exit: #{body}" + parser = new xml.SaxParser (cb) -> + cb.onStartElementNS (name, attrs, prefix, uri, namespaces) -> + element = name + content = '' + cb.onCharacters (str) -> + content += str + cb.onEndElementNS (name, attrs, prefix, uri, namespaces) -> + if element is 'key' + done = true + auth.sk = content + console.log("got key \"#{content}\"") + callback null, content + else if element is 'error' + done = true + callback "login failed: \"#{content}\"" + # ignore other tags stuff in there + cb.onEndDocument huh + parser.parseString body + timeout 1, huh + + +exports.login = login # fixme remove this from the API and call it automatically +exports.new_auth_token = new_auth_token +exports.save_auth = save_auth diff --git a/auth.coffee b/auth.coffee deleted file mode 100644 index 1b776ab..0000000 --- a/auth.coffee +++ /dev/null @@ -1,81 +0,0 @@ -fs = require 'fs' -http = require 'http' -crypto = require 'crypto' -xml = require 'node-xml' - -auth = {} - -md5 = (str) -> - sum = crypto.createHash 'md5' - sum.update str - return sum.digest 'hex' - -new_auth_token = (user, pass) -> - token = md5(user + md5(pass)) - auth.user = user - auth.token = token - return token - -auth_file = "#{process.env.HOME}/.libre.fm-cmus.auth" -save_auth = (user, pass, callback) -> - token = new_auth_token(user, pass) - text = JSON.stringify user: user, token: new_auth_token(user, pass) - fs.writeFile auth_file, text, 'utf8', callback - return token - -# load login credentials from settings file -load_auth = (callback) -> - if auth.user and auth.token - callback auth - return - - fs.readFile auth_file, 'utf8', (err, data) -> - if err - callback err - else - callback null, JSON.parse data - -# login and get a session key -login = (callback) -> - load_auth (err, auth) -> - return callback(err) if err? - - http.get { host: 'alpha.libre.fm', port: 80, path: "/2.0/?method=auth.getmobilesession&username=#{auth.user}&authToken=#{auth.token}"}, (res) -> - if res.statusCode != 200 - console.log "login response code: #{res.statusCode}" - callback "login response code: #{res.statusCode}" - return - - res.setEncoding 'utf8' - body = '' - res.on 'data', (chunk) -> - body += chunk - res.on 'end', -> - cur_tag = '' - done = false - parser = new xml.SaxParser (cb) -> - cb.onStartElementNS (name, attrs, prefix, uri, namespaces) -> - cur_tag = name - cb.onCharacters (str) -> - if cur_tag is 'key' - done = true - auth.sk = str - console.log("got key \"#{str}\"") - callback null, str - else if cur_tag is 'error' - done = true - callback "login failed: #{str}" - # ignore other stuff in there - unless parser.parse body, true - done = true - callback "login server response wouldn't parse as XML." - console.log "login server sent us bad XML: #{body}" - - unless done - callback "Couldn't understand response from login server" - - - -exports.login = login # fixme remove this from the API and call it automatically -exports.new_auth_token = new_auth_token -exports.save_auth = save_auth -- 1.7.10.4