Screencast: APIs schützen

Es gibt viele Möglichkeiten den Zugriff auf APIs zu schützen. In diesem Screencast zeigt Ryan erst einen möglichen Schutz über HTTP Basic Auth und später über Tokens die als Parameter übertragen werden.

Be Sociable, Share!
 

Downloads in verschiedenen Formaten:

mp4
mp4
webm
ogg

 

Resourcen:

terminal

rails g model api_key access_token
curl http://localhost:3000/api/products -I
curl http://localhost:3000/api/products -u 'admin:secret'
curl 'http://localhost:3000/api/products?access_token=123' -I
curl http://localhost:3000/api/products -H 'Authorization: Token token="c576f0136149a2e2d9127b3901015545"'

api/v1/products_controller.rb

# http_basic_authenticate_with name: "admin", password: "secret"
before_filter :restrict_access

private

# def restrict_access
#   api_key = ApiKey.find_by_access_token(params[:access_token])
#   head :unauthorized unless api_key
# end

def restrict_access
  authenticate_or_request_with_http_token do |token, options|
    ApiKey.exists?(access_token: token)
  end
end

models/api_key.rb

before_create :generate_access_token

private

def generate_access_token
  begin
    self.access_token = SecureRandom.hex
  end while self.class.exists?(access_token: access_token)
end
Be Sociable, Share!

Eingestellt am 31.05.2012 um 11:37

Kategorie: Tutorials

Be Sociable, Share!

Kommentare für diesen Artikel wurden geschlossen.

Suchen auf rubyonrails.de

Aktuelle Rails Version + Abhängigkeiten:

Gem rails-3.0.0
actionmailer (= 3.0.0, runtime)
actionpack (= 3.0.0, runtime)
activerecord (= 3.0.0, runtime)
activeresource (= 3.0.0, runtime)
activesupport (= 3.0.0, runtime)
bundler (~> 1.0.0, runtime)
railties (= 3.0.0, runtime)

Rails auf Rubyforge
Rails auf Github