简体   繁体   English

使用现有 Rails 应用程序验证 Flask API

[英]Authenticating Flask API with an existing Rails App

I have an existing web application developed with Ruby on Rails and I would like to create a simple Flask API to make request over the existing tables and authenticate the API with the preexisting users. I have an existing web application developed with Ruby on Rails and I would like to create a simple Flask API to make request over the existing tables and authenticate the API with the preexisting users. What would be the best approach?最好的方法是什么?

I ended up creating an api generator module and requiring it in the user model, setting a before_validation so when a new user signs up the api key is generated.我最终创建了一个 api 生成器模块,并在用户 model 中要求它,设置了 before_validation,以便在新用户注册时生成 api 密钥。 Then the user can authenticate against the flask api with the api key.然后,用户可以使用 api 密钥对 flask api 进行身份验证。

# lib/apy_key.rb

module ApiKey
  def self.generator
    SecureRandom.base64(40)
  end
end
# models/user.rb
class User < ActiveRecord::Base
...
  require './lib/api_key.rb'
  validates_presence_of :api_key
  validates_uniqueness_of :api_key
  before_validation :set_api_key

  private
    def set_api_key
      self.api_key = ApiKey.generator
    end

end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM