通过 ruby​​ 脚本设计身份验证

发布于 2024-11-27 20:02:01 字数 672 浏览 0 评论 0原文

我有一个小型应用程序,并且我会让一些外部应用程序通过 http 将数据放置到此服务中。我已经让它工作了,但没有身份验证。在门户中,我使用 devise,我的问题是:如何(所需的示例)从 ruby​​ 脚本级别对门户进行身份验证?要首先在以下脚本中添加什么来进行身份验证?我想用设计保护这个控制器,然后我需要以下脚本的身份验证部分。

require "net/http"
require "json"

@host = "localhost"
@port = 3000
@post_ws = "/external/rd"

@req_body = {
"device" => {
  "name" => "device_json", 
  "operating_system_id" => "7", 
  "hash_string" => "jfsg3k4ovj0j02jv", 
  "user_id" => "1"
}
}.to_json

req = Net::HTTP::Post.new(@post_ws, initheader = {'Content-Type' =>'application/json'})
req.body = @req_body
response = Net::HTTP.new(@host, @port).start {|http| http.request(req) }

问候, 马特乌什

I have small application, and I will have some external applications put data to this service over http with rest. I already have it working but without authentication. In portal I use devise, and my question is: how to (example desired) authenticate to portal from ruby script level? What to add to following script to authenticate first? I want to protect this controller with devise and then I need authentication part to following script.

require "net/http"
require "json"

@host = "localhost"
@port = 3000
@post_ws = "/external/rd"

@req_body = {
"device" => {
  "name" => "device_json", 
  "operating_system_id" => "7", 
  "hash_string" => "jfsg3k4ovj0j02jv", 
  "user_id" => "1"
}
}.to_json

req = Net::HTTP::Post.new(@post_ws, initheader = {'Content-Type' =>'application/json'})
req.body = @req_body
response = Net::HTTP.new(@host, @port).start {|http| http.request(req) }

Regards,
Mateusz

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

¢蛋碎的人ぎ生 2024-12-04 20:02:01

这是解决方案:

我使用了 devise 中的 token_authenticatable 。

这里是一个很棒的回答如何用json实现。我遇到了一些麻烦,并在此问题中描述了它们。也有答案。

这是示例代码:

require "net/http"
require "json"

@host = "localhost"
@port = 3000
@post_sign = "/users/sign_in.json"
@post_ws = "/external/rd"

@req_sign = {
"user" => {
  "email" => "[email protected]", 
  "password" => "123456" 
}
}.to_json

sig = Net::HTTP::Post.new(@post_sign, initheader = {'Content-Type' => 'application/json'})
sig.body = @req_sign


http = Net::HTTP.new(@host, @port).start
resp1 = http.request(sig)
puts "Response: #{resp1.code} , Message: #{resp1.message} , Body: #{resp1.body}"

if resp1.code == "200" then 
  puts "logged in"
  json_resp = JSON.parse(resp1.body)
  @auth_token = json_resp['auth_token']

  @req_body = {
  "device" => {
    "name" => "device_json", 
    "operating_system_id" => "7", 
    "hash_string" => "jfsg3k4ovj0j02jv" 
  }, 
  "auth_token" => @auth_token 
  }.to_json
  req = Net::HTTP::Post.new(@post_ws, initheader = {'Content-Type' =>'application/json'})
  req.body = @req_body

  response = http.request(req)
  puts "Response: #{response.code} , Message: #{response.message} , Body: #{response.body}"
end

问候,
马特乌什

Here is solution:

I used token_authenticatable from devise.

Here is one great answer how to implement it with json. I had some trouble and described them in this question. There is also answer.

Here goes example code:

require "net/http"
require "json"

@host = "localhost"
@port = 3000
@post_sign = "/users/sign_in.json"
@post_ws = "/external/rd"

@req_sign = {
"user" => {
  "email" => "[email protected]", 
  "password" => "123456" 
}
}.to_json

sig = Net::HTTP::Post.new(@post_sign, initheader = {'Content-Type' => 'application/json'})
sig.body = @req_sign


http = Net::HTTP.new(@host, @port).start
resp1 = http.request(sig)
puts "Response: #{resp1.code} , Message: #{resp1.message} , Body: #{resp1.body}"

if resp1.code == "200" then 
  puts "logged in"
  json_resp = JSON.parse(resp1.body)
  @auth_token = json_resp['auth_token']

  @req_body = {
  "device" => {
    "name" => "device_json", 
    "operating_system_id" => "7", 
    "hash_string" => "jfsg3k4ovj0j02jv" 
  }, 
  "auth_token" => @auth_token 
  }.to_json
  req = Net::HTTP::Post.new(@post_ws, initheader = {'Content-Type' =>'application/json'})
  req.body = @req_body

  response = http.request(req)
  puts "Response: #{response.code} , Message: #{response.message} , Body: #{response.body}"
end

Regards,
Mateusz

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文