Skip to content

Commit 2f4f352

Browse files
committed
Authentication implemented, it is now possible to call methods that require authentication within the API.
1 parent a54fa22 commit 2f4f352

File tree

1 file changed

+71
-44
lines changed

1 file changed

+71
-44
lines changed

lib/flickr.js

+71-44
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,21 @@ var sys = require("sys"),
44
md5 = require('md5');
55
var flickr = http.createClient(80, "api.flickr.com");
66

7-
function generate_signature(shared_secret, arguments) {
7+
flickr.Request= function Request(api_key, shared_secret, auth_token) {
8+
this._configure(api_key, shared_secret, auth_token);
9+
};
10+
11+
flickr.Request.prototype._configure= function(api_key, shared_secret, auth_token) {
12+
this.api_key= api_key;
13+
this.shared_secret= shared_secret;
14+
this.auth_token= auth_token;
15+
};
16+
17+
flickr.Request.prototype.setAuthenticationToken= function(auth_token) {
18+
this._configure(this.api_key, this.shared_secret, auth_token);
19+
};
20+
21+
flickr.Request.prototype.generateSignature= function(shared_secret, arguments) {
822
var argument_pairs= [];
923
for(var key in arguments ) {
1024
argument_pairs[argument_pairs.length]= [key, arguments[key]];
@@ -21,9 +35,9 @@ function generate_signature(shared_secret, arguments) {
2135
}
2236
var sig= shared_secret+args;
2337
return md5.md5(sig);
24-
}
38+
};
2539

26-
function create_flickr_request(method, arguments, api_key, shared_secret, sign, result_mapper) {
40+
flickr.Request.prototype.getRequestPromise= function(method, arguments, sign_it, result_mapper) {
2741
var promise= new process.Promise()
2842
var argumentString = "";
2943
var api_sig= undefined;
@@ -33,10 +47,11 @@ function create_flickr_request(method, arguments, api_key, shared_secret, sign,
3347
arguments.format= "json";
3448
arguments.nojsoncallback= "1";
3549
arguments["method"]= method;
36-
arguments.api_key= api_key;
50+
arguments.api_key= this.api_key;
51+
if( this.auth_token ) arguments.auth_token= this.auth_token;
3752

38-
if( shared_secret && sign) {
39-
api_sig= generate_signature(shared_secret, arguments);
53+
if( this.shared_secret && (sign_it || this.auth_token) ) {
54+
api_sig= this.generateSignature(this.shared_secret, arguments);
4055
if( api_sig ) {
4156
arguments.api_sig= api_sig;
4257
}
@@ -75,63 +90,67 @@ function create_flickr_request(method, arguments, api_key, shared_secret, sign,
7590
});
7691
});
7792
return promise;
78-
}
93+
};
7994

80-
flickr.Auth= function Auth(api_key, shared_secret) {
81-
this.api_key= api_key;
82-
this.shared_secret= shared_secret;
95+
flickr.Auth= function Auth(request) {
96+
this._request= request;
8397
};
8498

8599
flickr.Auth.prototype.getFrob= function() {
86-
return create_flickr_request("flickr.auth.getFrob",
87-
{}, this.api_key, this.shared_secret, true, function(res) { return res._content; });
100+
return this._request.getRequestPromise("flickr.auth.getFrob",
101+
{}, true, function(res) { return res._content; });
88102
};
89103

90104
flickr.Auth.prototype.getToken= function(frob) {
91-
return create_flickr_request("flickr.auth.getToken",
92-
{"frob":frob}, this.api_key, this.shared_secret, true);
105+
return this._request.getRequestPromise("flickr.auth.getToken",
106+
{"frob":frob}, true, function(res) {
107+
return {
108+
"token": res.token._content,
109+
"perms": res.perms._content,
110+
"user": res.user
111+
};
112+
});
113+
};
114+
115+
flickr.Blogs= function Blogs(request) {
116+
this._request= request;
117+
};
118+
flickr.Blogs.prototype.getList= function() {
119+
return this._request.getRequestPromise("flickr.blogs.getList",
120+
{}, true);
93121
};
94122

95123

96-
flickr.People= function People(api_key, shared_secret) {
97-
this.api_key= api_key;
98-
this.shared_secret= shared_secret;
124+
flickr.People= function People(request) {
125+
this._request= request;
99126
};
100127

101128
flickr.People.prototype.findByUsername= function(username){
102-
return create_flickr_request("flickr.people.findByUsername",
103-
{"username": username}, this.api_key, this.shared_secret);
129+
return this._request.getRequestPromise("flickr.people.findByUsername",
130+
{"username": username});
104131
};
105132

106-
flickr.Photos= function Photos(api_key, shared_secret) {
107-
this.api_key= api_key;
108-
this.shared_secret= shared_secret;
133+
flickr.Photos= function Photos(request) {
134+
this._request= request;
109135
};
110136

111137
flickr.Photos.prototype.getInfo= function(photo_id, secret) {
112138
var arguments= {"photo_id": photo_id};
113139
if( secret !== undefined ) arguments.secret= secret;
114-
115-
return create_flickr_request("flickr.photos.getInfo",
116-
arguments,
117-
this.api_key, this.shared_secret);
140+
141+
return this._request.getRequestPromise("flickr.photos.getInfo", arguments);
118142
};
119143

120-
flickr.Photosets= function Photosets(api_key, shared_secret) {
121-
this.api_key= api_key;
122-
this.shared_secret= shared_secret;
144+
flickr.Photosets= function Photosets(request) {
145+
this._request= request;
123146
};
124147

125148
flickr.Photosets.prototype.getInfo= function(photoset_id) {
126-
return create_flickr_request("flickr.photosets.getInfo",
127-
{"photoset_id": photoset_id},
128-
this.api_key, this.shared_secret);
149+
return this._request.getRequestPromise("flickr.photosets.getInfo", {"photoset_id": photoset_id});
129150
};
130151

131152
flickr.Photosets.prototype.getList= function(user_id) {
132-
return create_flickr_request("flickr.photosets.getList",
133-
{"user_id": user_id},
134-
this.api_key, this.shared_secret);
153+
return this._request.getRequestPromise("flickr.photosets.getList", {"user_id": user_id});
135154
};
136155

137156
flickr.Photosets.prototype.getPhotos= function(photoset_id, extras, privacy_filter, per_page, page, media) {
@@ -142,18 +161,26 @@ flickr.Photosets.prototype.getPhotos= function(photoset_id, extras, privacy_filt
142161
if( per_page !== undefined ) arguments.per_page= per_page;
143162
if( page !== undefined ) arguments.page= page;
144163
if( media !== undefined ) arguments.media= media;
145-
return create_flickr_request("flickr.photosets.getPhotos",
146-
arguments, this.api_key, this.shared_secret);
164+
return this._request.getRequestPromise("flickr.photosets.getPhotos", arguments);
165+
};
147166

167+
flickr.FlickrAPI= function FlickrAPI(api_key, shared_secret, auth_token) {
168+
this._configure(api_key, shared_secret, auth_token);
148169
};
149170

150-
flickr.FlickrAPI= function FlickrAPI(api_key, shared_secret) {
151-
this.api_key= api_key;
152-
this.shared_secret= shared_secret;
153-
this.people= new flickr.People(api_key,shared_secret);
154-
this.photos= new flickr.Photos(api_key,shared_secret);
155-
this.photosets= new flickr.Photosets(api_key,shared_secret);
156-
this.auth= new flickr.Auth(api_key, shared_secret);
171+
flickr.FlickrAPI.prototype._configure= function(api_key, shared_secret, auth_token) {
172+
this._request= new flickr.Request(api_key, shared_secret, auth_token);
173+
174+
this.people= new flickr.People(this._request);
175+
this.photos= new flickr.Photos(this._request);
176+
this.photosets= new flickr.Photosets(this._request);
177+
this.auth= new flickr.Auth(this._request);
178+
this.blogs= new flickr.Blogs(this._request);
179+
};
180+
181+
182+
flickr.FlickrAPI.prototype.setAuthenticationToken= function(auth_token) {
183+
this._request.setAuthenticationToken(auth_token);
157184
};
158185

159186
flickr.FlickrAPI.prototype.getLoginUrl= function(permissions, frob) {

0 commit comments

Comments
 (0)