flickr-login 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ Gemfile.lock
2
+ pkg/
3
+ *.DS_Store
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Janko Marohnić
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,145 @@
1
+ # Flickr Login
2
+
3
+ ## About
4
+
5
+ This gem is a simple Rack endpoint that provides Flickr authentication in your
6
+ web application. You just mount it on your web application and it does the rest of the work.
7
+
8
+ This gem is an alternative to [omniauth-flickr](https://github.com/timbreitkreutz/omniauth-flickr).
9
+ They both provide similar results, the main difference is that `flickr-login` is much more
10
+ lightweight. If you don't care about that, I would highly suggest that you go with
11
+ `omniauth-flickr`, because `omniauth` is a great standard for authentication.
12
+
13
+ If you intend to use the user's access token for communication with Flickr's
14
+ API, you'll want to use one of this 2 gems:
15
+
16
+ - [flickraw](https://github.com/hanklords/flickraw)
17
+ - [flickrie](https://github.com/janko-m/flickrie)
18
+
19
+ ## Installation
20
+
21
+ Put it into your Gemfile:
22
+
23
+ ```ruby
24
+ gem "flickr-login", require: "flickr/login"
25
+ ```
26
+
27
+ And run `bundle install`.
28
+
29
+ ## Setup
30
+
31
+ You have to be in possession of your API key and shared secret. If you don't have them yet,
32
+ you can apply for them [here](http://www.flickr.com/services/apps/create/apply).
33
+ In the setup, just replace `API_KEY` and `SHARED_SECRET` with real values.
34
+
35
+ ### Rails
36
+
37
+ This is an example of how you can the gem in **Rails 3** (in Rails 2 it's probably
38
+ similar).
39
+
40
+ ```ruby
41
+ # config/application.rb
42
+ module YourApp
43
+ class Application < Rails::Application
44
+ # ...
45
+ config.flickr_login = Flickr::Login.new "API_KEY", "SHARED_SECRET"
46
+ # ...
47
+ end
48
+ end
49
+ ```
50
+ ```ruby
51
+ # config/routes.rb
52
+ YourApp::Application.routes.draw do
53
+ # ...
54
+ flickr = YourApp::Application.config.flickr_login
55
+ flickr_endpoint = flickr.login_handler(return_to: "/any-path")
56
+
57
+ mount flickr_endpoint => '/login', as: :login
58
+ # ...
59
+ end
60
+ ```
61
+
62
+ ### Sinatra
63
+
64
+ In Sinatra this is being put in your `config.ru`, which probably looks
65
+ something like this:
66
+
67
+ ```ruby
68
+ # config.ru
69
+ require './app'
70
+ run Sinatra::Application
71
+ ```
72
+
73
+ Now you mount the Rack endpoint like this
74
+
75
+ ```ruby
76
+ # config.ru
77
+ require './app'
78
+ require 'flickr/login'
79
+
80
+ flickr = Flickr::Login.new "API_KEY", "SHARED_SECRET"
81
+ flickr_endpoint = flickr.login_handler(return_to: "/any-path")
82
+
83
+ use Rack::Session::Cookie
84
+ run Rack::URLMap.new "/" => Sinatra::Application,
85
+ "/login" => flickr_endpoint
86
+ ```
87
+
88
+ That's it. Just enable sessions in your `app.rb`:
89
+
90
+ ```ruby
91
+ # app.rb
92
+ enable :sessions
93
+ ```
94
+
95
+ ## What it does
96
+
97
+ The user will first get redirected to Flickr to approve your application. The
98
+ user is then redirected back to your app (back to the path specified with `:return_to`),
99
+ with `session[:flickr_access_token]` and `session[:flickr_user]` filled in.
100
+
101
+ - `session[:flickr_access_token]` – an array of access token and access secret
102
+ - `session[:flickr_user]` – a hash of information about the authenticated user
103
+
104
+ ## Configuration
105
+
106
+ Available options for `Flickr::Login` are:
107
+
108
+ - `:return_to` – where the user is redirected to after authentication (defaults to `"/"`)
109
+ - `:site` – the API endpoint that is used (defaults to [http://www.flickr.com/services](http://www.flickr.com/services))
110
+
111
+ You can also set the permissions you're asking from the user. You do this by passing the
112
+ `perms` GET parameter in the URL. For example, going to `http://localhost:9393/login?perms=delete`
113
+ would ask the user for "delete" permissions. You can ask the user for "read", "write" or "delete" permissions.
114
+
115
+ ## Helpers
116
+
117
+ The `Flickr::Login::Helpers` module adds these methods to your app:
118
+
119
+ - `#flickr_user` (Hash) – The information about the user who just authenticated
120
+ - `#flickr_access_token` (Array) – The access token and secret
121
+ - `#flickr_clear` – Erases the session that was filled after authentication, effectively logging out the user
122
+
123
+ In **Rails** you can include the module in your controller:
124
+
125
+ ```ruby
126
+ # app/controllers/session_controller.rb
127
+ class SessionController < ApplicationController
128
+ include Flickr::Login::Helpers
129
+ end
130
+ ```
131
+
132
+ In **Sinatra** you can just call the `helpers` method:
133
+
134
+ ```ruby
135
+ helpers Flickr::Login::Helpers
136
+ ```
137
+
138
+ ## Credits
139
+
140
+ This gem is almost a direct copy of **@mislav**'s [twitter-login](https://github.com/mislav/twitter-login)
141
+ and [facebook-login](https://github.com/mislav/facebook) gems.
142
+
143
+ ## License
144
+
145
+ [MIT](https://github.com/janko-m/flickr-login/blob/master/LICENSE)
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+ Gem::Specification.new do |gem|
3
+ gem.name = "flickr-login"
4
+ gem.authors = ["Janko Marohnić"]
5
+ gem.email = ["janko.marohnic@gmail.com"]
6
+ gem.description = %q{This is a Rack endpoint that provides Flickr authentication}
7
+ gem.summary = %q{}
8
+ gem.homepage = "https://github.com/janko-m/flickr-login"
9
+
10
+ gem.files = `git ls-files`.split($\)
11
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
12
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
13
+ gem.require_paths = ["lib"]
14
+ gem.version = "0.0.1"
15
+
16
+ gem.add_dependency "oauth"
17
+ end
@@ -0,0 +1,88 @@
1
+ require 'oauth'
2
+ require 'uri'
3
+
4
+ module Flickr
5
+ class Login
6
+ DEFAULTS = {
7
+ site: "http://www.flickr.com/services",
8
+ return_to: "/"
9
+ }
10
+
11
+ def initialize(api_key, shared_secret, options = {})
12
+ @api_key, @shared_secret = api_key, shared_secret
13
+ @options = DEFAULTS.merge(options)
14
+ end
15
+
16
+ def call(env)
17
+ request = Request.new(env)
18
+
19
+ unless request[:oauth_verifier]
20
+ redirect_to_flickr(request)
21
+ else
22
+ handle_flickr_authorization(request)
23
+ end
24
+ end
25
+
26
+ def login_handler(options = {})
27
+ @options.update(options)
28
+ self
29
+ end
30
+
31
+ module Helpers
32
+ def flickr_user
33
+ session[:flickr_user]
34
+ end
35
+
36
+ def flickr_access_token
37
+ session[:flickr_access_token]
38
+ end
39
+
40
+ def flickr_clear
41
+ [:flickr_user, :flickr_access_token].each do |key|
42
+ session.delete(key)
43
+ end
44
+ end
45
+ end
46
+
47
+ class Request < ::Rack::Request
48
+ def url_for(path)
49
+ url = scheme + '://' + host
50
+ if (scheme == 'https' and port != 443) or (scheme == 'http' and port != 80)
51
+ url << ":#{port}"
52
+ end
53
+ url << path
54
+ end
55
+ end
56
+
57
+ private
58
+
59
+ def redirect_to_flickr(request)
60
+ callback_url = URI.parse(request.url).tap { |url| url.query = nil }
61
+ request_token = consumer.get_request_token(oauth_callback: callback_url.to_s)
62
+ request.session[:flickr_request_token] = [request_token.token, request_token.secret]
63
+ authorize_params = request[:perms] ? {perms: request[:perms]} : {}
64
+ redirect request_token.authorize_url(authorize_params)
65
+ end
66
+
67
+ def handle_flickr_authorization(request)
68
+ request_token = renew_request_token(request)
69
+ access_token = request_token.get_access_token(oauth_verifier: request[:oauth_verifier])
70
+ request.session[:flickr_access_token] = [access_token.token, access_token.secret]
71
+ request.session[:flickr_user] = access_token.params
72
+ redirect request.url_for(@options[:return_to])
73
+ end
74
+
75
+ def renew_request_token(request)
76
+ rtoken, rsecret = request.session.delete(:flickr_request_token)
77
+ OAuth::RequestToken.new(consumer, rtoken, rsecret)
78
+ end
79
+
80
+ def consumer
81
+ @consumer ||= OAuth::Consumer.new @api_key, @shared_secret, site: @options[:site]
82
+ end
83
+
84
+ def redirect(url)
85
+ ['302', {'Location' => url, 'Content-type' => 'text/plain'}, []]
86
+ end
87
+ end
88
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flickr-login
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Janko Marohnić
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: oauth
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: This is a Rack endpoint that provides Flickr authentication
31
+ email:
32
+ - janko.marohnic@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE
40
+ - README.md
41
+ - Rakefile
42
+ - flickr-login.gemspec
43
+ - lib/flickr/login.rb
44
+ homepage: https://github.com/janko-m/flickr-login
45
+ licenses: []
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 1.8.23
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: ''
68
+ test_files: []