Skip to content

Commit

Permalink
Data type for storing credentials for a remote system
Browse files Browse the repository at this point in the history
Store the OAuth credentials for a remote system for a user.
  • Loading branch information
Evan Prodromou committed Sep 14, 2012
1 parent e9f92a8 commit 5538271
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 1 deletion.
48 changes: 48 additions & 0 deletions lib/model/credentials.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Credentials for a remote system
//
// Copyright 2012 StatusNet Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

var databank = require("databank"),
Stamper = require("../stamper").Stamper,
_ = require("underscore"),
DatabankObject = databank.DatabankObject,
NoSuchThingError = databank.NoSuchThingError;

var Credentials = DatabankObject.subClass("credentials");

Credentials.schema = {
pkey: "host_and_id",
fields: ["host",
"id",
"client_id",
"client_secret",
"expires_at",
"created",
"updated"],
indices: ["host", "id", "client_id"]
};

Credentials.beforeCreate = function(props, callback) {
props.created = props.updated = Stamper.stamp();
callback(null, props);
};

Credentials.prototype.beforeUpdate = function(props, callback) {
props.updated = Stamper.stamp();
callback(null, props);
};

exports.Credentials = Credentials;

4 changes: 3 additions & 1 deletion lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ var _ = require("underscore"),
Client = require("./model/client").Client,
RequestToken = require("./model/requesttoken").RequestToken,
AccessToken = require("./model/accesstoken").AccessToken,
Nonce = require("./model/nonce").Nonce;
Nonce = require("./model/nonce").Nonce,
Credentials = require("./model/credentials").Credentials;

var getSchema = function() {

Expand All @@ -42,6 +43,7 @@ var getSchema = function() {
schema[RequestToken.type] = RequestToken.schema;
schema[AccessToken.type] = AccessToken.schema;
schema[Nonce.type] = Nonce.schema;
schema[Credentials.type] = Credentials.schema;

for (i = 0; i < ActivityObject.objectTypes.length; i++) {
type = ActivityObject.objectTypes[i];
Expand Down
67 changes: 67 additions & 0 deletions test/credentials-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// credentials-test.js
//
// Test the credentials module
//
// Copyright 2012, StatusNet Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

var assert = require("assert"),
vows = require("vows"),
databank = require("databank"),
Step = require("step"),
URLMaker = require("../lib/urlmaker").URLMaker,
modelBatch = require("./lib/model").modelBatch,
Databank = databank.Databank,
DatabankObject = databank.DatabankObject;

var suite = vows.describe("credentials module interface");

var testSchema = {
pkey: "host_and_id",
fields: ["host",
"id",
"client_id",
"client_secret",
"expires_at",
"created",
"updated"],
indices: ["host", "id", "client_id"]
};

var testData = {
"create": {
host: "social.example",
id: "acct:user@comment.example",
client_id: "AAAAAA",
client_secret: "123456",
expires_at: 0
},
"update": {
expires_at: 1
}
};

var mb = modelBatch("credentials", "Credentials", testSchema, testData);

mb["When we require the credentials module"]
["and we get its Credentials class export"]
["and we create a credentials instance"]
["auto-generated fields are there"] = function(err, created) {
assert.isString(created.created);
assert.isString(created.updated);
};

suite.addBatch(mb);

suite["export"](module);

0 comments on commit 5538271

Please sign in to comment.