JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
hash and save auth credentials
[libre-fm-client-daemon.git] / auth.coffee
diff --git a/auth.coffee b/auth.coffee
new file mode 100644 (file)
index 0000000..619e169
--- /dev/null
@@ -0,0 +1,18 @@
+fs = require 'fs'
+crypto = require 'crypto'
+
+md5 = (str) ->
+       sum = crypto.createHash 'md5'
+       sum.update str
+       return sum.digest 'hex'
+
+new_auth_token = (user, pass) -> md5(user + md5(pass))
+
+save_auth = (user, pass, callback) ->
+       token = new_auth_token(user, pass)
+       text = JSON.stringify user: user, token: new_auth_token(user, pass)
+       fs.writeFile "#{process.env.HOME}/.libre.fm-cmus.auth", text, 'utf8', callback
+       return token
+
+exports.new_auth_token = new_auth_token
+exports.save_auth = save_auth