remove console.logs
[xtuple] / enyo-client / extensions / source / oauth2 / client / models / oauth2.js
1 /*jshint indent:2, curly:true, eqeqeq:true, immed:true, latedef:true,
2 newcap:true, noarg:true, regexp:true, undef:true, strict:true, trailing:true,
3 white:true*/
4 /*global XT:true, XM:true, XV:true, Backbone:true, _:true, window:true */
5
6 (function () {
7   "use strict";
8
9   XT.extensions.oauth2.initModels = function () {
10
11     //
12     // MODELS
13     //
14
15     XM.Oauth2client = XM.Model.extend({
16
17       recordType: "XM.Oauth2client",
18
19       autoFetchId: true,
20
21       defaults: function () {
22         return {
23           isActive: true
24         };
25       },
26
27       readOnlyAttributes: [
28         "clientID",
29         "clientSecret",
30         "issued",
31         "organization"
32       ],
33
34       bindEvents: function () {
35         XM.Model.prototype.bindEvents.apply(this, arguments);
36         this.on('statusChange', this.statusDidChange);
37         this.on('change:clientType', this.clientTypeDidChange);
38       },
39
40       // clientType must not be editable once first saved.
41       statusDidChange: function () {
42         this.setReadOnly('clientType', this.getStatus() !== XM.Model.READY_NEW);
43
44         if (this.getStatus() === XM.Model.READY_NEW) {
45           this.set('clientID', XT.getOrganizationPath().substring(1) + "_" + XT.generateUUID());
46           // XXX the secret is only relevant for websites, but we generate it here
47           // for both because it's required to be unique on the DB level
48           // secret keys only seem to be applicable for website clients, so we might
49           // not want to make it unique
50           this.set('clientSecret', XT.generateUUID());
51           this.set('issued', new Date());
52         }
53       },
54
55       clientTypeDidChange: function () {
56         this.set("delegatedAccess", this.get("clientType") === 'jwt bearer');
57       },
58
59       save: function (key, value, options) {
60         // Handle both `"key", value` and `{key: value}` -style arguments.
61         if (_.isObject(key) || _.isEmpty(key)) {
62           options = value;
63         }
64         options = options ? _.clone(options) : {};
65
66         var success = options.success,
67           status = this.getStatus(),
68           that = this;
69
70         options.success = function (model, resp, options) {
71           if (status === XM.Model.READY_NEW && that.get("clientType") === 'jwt bearer') {
72             // Download the private key.
73             XV.downloadURL(XT.getOrganizationPath() + '/oauth/generate-key?id=' + that.id);
74           }
75
76           if (success) { success(model, resp, options); }
77         };
78
79         // Handle both `"key", value` and `{key: value}` -style arguments.
80         if (_.isObject(key) || _.isEmpty(key)) {
81           value = options;
82         }
83
84         if (status === XM.Model.READY_NEW && that.get("clientType") === 'jwt bearer') {
85           // The order of operations for a new jwt bearer is
86           // 1. Notify the user that the download is coming
87           // 2. Save normally
88           // 3. Open the download tab.
89           //
90           // It's a little curious that 2 happens after 1, but the notify listener
91           // on the workspace gets destroyed by the time we would need it otherwise.
92           that.notify("_generatingPrivateKey".loc(), {callback: function () {
93             XM.Model.prototype.save.call(that, key, value, options);
94           }});
95         } else {
96           XM.Model.prototype.save.call(this, key, value, options);
97         }
98       }
99
100     });
101
102     XM.Oauth2clientRedirs = XM.Model.extend({
103
104       recordType: "XM.Oauth2clientRedirs",
105
106       autoFetchId: true
107
108     });
109
110     //
111     // COLLECTIONS
112     //
113
114     XM.Oauth2clientCollection = XM.Collection.extend({
115       model: XM.Oauth2client
116     });
117
118     XM.Oauth2clientRedirsCollection = XM.Collection.extend({
119       model: XM.Oauth2clientRedirs
120     });
121   };
122 }());