Ruby on rails Rails:找不到'*_路径';
我正在尝试使用html按钮从主页重定向到我的Azure登录页面 如果我在浏览器中输入localhost/3000/authentication/login,它将转到Azure web应用程序 这就是我试图通过以下方式实现的目标:Ruby on rails Rails:找不到'*_路径';,ruby-on-rails,ruby,oauth-2.0,routes,Ruby On Rails,Ruby,Oauth 2.0,Routes,我正在尝试使用html按钮从主页重定向到我的Azure登录页面 如果我在浏览器中输入localhost/3000/authentication/login,它将转到Azure web应用程序 这就是我试图通过以下方式实现的目标: <%= button_to 'Login', authentication_login_path, method: :get %> 应用程序和家庭控制器目前只有空白功能 我尝试过rake | grep身份验证,它包含正确的路径: authenticati
<%= button_to 'Login', authentication_login_path, method: :get %>
应用程序和家庭控制器目前只有空白功能
我尝试过rake | grep身份验证,它包含正确的路径:
authentication_login GET /authentication/login(.:format) authentication#index
因此,我不确定我的home.html.erg文件中不识别身份验证路径
这是身份验证控制器。我正在尝试执行index方法,因为这将开始身份验证过程
require 'oauth2'
class AuthenticationController < ApplicationController
# You need to configure a tenant at Azure Active Directory(AAD) to register web app and web service app
# You will need two entries for these app at the AAD portal
# You will put clientid and clientsecret for your web app here
# ResourceId is the webservice that you registered
# RedirectUri is registered for your web app
CLIENT_ID = '56938f79-a23e-4f3f-a033-d23546d9056f'
CLIENT_SECRET = '5j8Hv8U1x_l-t047OZq9~LmK~kMdobV3rm'
AUTHORITY = 'https://login.windows.net/'
AUTHORIZE_URL = "https://beautytruth.b2clogin.com/beautytruth.onmicrosoft.com/B2C_1_btSignInSignOut/oauth2/v2.0/authorize"
TOKEN_URL = "https://beautytruth.b2clogin.com/beautytruth.onmicrosoft.com/B2C_1_btSignInSignOut/oauth2/v2.0/token"
RESOURCE_ID = '/subscriptions/eb589fa5-ed57-4e10-81c9-32e4284af10c/resourceGroups/btAdvertisingNetwork' #ResourceId or ResourceURI that you registered at Azure Active Directory
REDIRECT_URI = 'http://localhost:3000/welcome/callback'
def index
update_token
if session['access_token']
puts "Auth has been checked"
# show main page and use token
redirect_to
else
# start authorization
client = get_client
a = client.auth_code.authorize_url(:client_id => CLIENT_ID, :resource => RESOURCE_ID, :redirect_uri => REDIRECT_URI)
redirect_to(a)
end
end
def callback
begin
@code = params[:code]
client = get_client
# post token to mobile service api
#token = client.auth_code.get_token(CGI.escape(@code), :redirect_uri => REDIRECT_URI)
# id_token token.params["id_token"]
#multi resource token token.params["resource"]
token = client.auth_code.get_token(@code, :redirect_uri => REDIRECT_URI, )
session['access_token'] = token.token
session['refresh_token'] = token.refresh_token
session['expire_at'] = token.expire_at
session['instance_url'] = token.params['instance_url']
redirect '/'
rescue => exception
output = '<html><body><p>'
output += "Exception: #{exception.message}<br/>"+exception.backtrace.join('<br/>')
output += '</p></body></html>'
end
end
def update_token
puts "update token inside"
token = session['access_token']
refresh_token = session['refresh_token']
expire_at = session['expire_at']
@access_token = OAuth2::AccessToken.from_hash(get_client, { :access_token => token, :refresh_token => refresh_token, :expire_at => expire_at, :header_format => 'Bearer %s' } )
if @access_token.expired?
puts "refresh token"
@access_token = @access_token.refresh!
session['access_token'] = @access_token.token
session['refresh_token'] = @access_token.refresh_token
session['expire_at'] = @access_token.expire_at
session['instance_url'] = @access_token.params['instance_url']
end
end
# send post request to webservice to send token and create a post request
def use_token
# we got the token and now it will posted to the web service in the header
# you can specify additional headers as well
# token is included by default
update_token
conn = Faraday.new(:url => 'https://btadvertisingplatform.azurewebsites.net/') do |faraday|
faraday.request :url_encoded # form-encode POST params
faraday.response :logger # log requests to STDOUT
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
end
response = conn.get do |req|
req.url '/api/WorkItem'
req.headers['Content-Type'] = 'application/json'
req.headers['Authorization'] = 'Bearer '+@access_token.token
end
@out = response.body
end
def get_client
client = OAuth2::Client.new(CLIENT_ID, CLIENT_SECRET, :site => AUTHORITY, :authorize_url => AUTHORIZE_URL, :token_url => TOKEN_URL )
client
end
end
需要“oauth2”
类AuthenticationController<应用程序控制器
#您需要在Azure Active Directory(AAD)上配置租户以注册web应用程序和web服务应用程序
#在AAD门户中,这些应用程序需要两个条目
#您将在此处放置web应用程序的clientid和clientsecret
#ResourceId是您注册的Web服务
#已为您的web应用注册重定向URI
客户ID='56938f79-a23e-4f3f-a033-d23546d9056f'
客户机密='5j8Hv8U1x_l-t047OZq9~LmK~kMdobV3rm'
权威人士https://login.windows.net/'
授权URL=”https://beautytruth.b2clogin.com/beautytruth.onmicrosoft.com/B2C_1_btSignInSignOut/oauth2/v2.0/authorize"
令牌\u URL=”https://beautytruth.b2clogin.com/beautytruth.onmicrosoft.com/B2C_1_btSignInSignOut/oauth2/v2.0/token"
RESOURCE_ID='/subscriptions/eb589fa5-ed57-4e10-81c9-32e4284af10c/resourceGroups/btAdvertisingNetwork'#您在Azure Active Directory中注册的ResourceId或ResourceURI
重定向http://localhost:3000/welcome/callback'
def索引
更新令牌
如果会话['access_token']
放入“已检查身份验证”
#显示主页并使用令牌
重定向到
其他的
#启动授权
client=get\u client
a=client.auth\u code.authorize\u url(:client\u id=>client\u id,:resource=>resource\u id,:redirect\u uri=>redirect\u uri)
将_重定向到(a)
结束
结束
def回调
开始
@代码=参数[:代码]
client=get\u client
#向移动服务api发布令牌
#token=client.auth\u code.get\u令牌(CGI.escape(@code),:redirect\u uri=>redirect\u uri)
#id_令牌。参数[“id_令牌”]
#多资源令牌。参数[“资源”]
token=client.auth\u code.get\u令牌(@code,:redirect\u uri=>redirect\u uri,)
会话['access_token']=token.token
会话['refresh\u token']=token.refresh\u token
会话['expire\u at']=token.expire\u at
会话['instance\u url']=token.params['instance\u url']
重定向“/”
rescue=>异常
输出=''
输出+=“异常:#{Exception.message}
”+Exception.backtrace.join(“
”)
输出+=''
结束
结束
def更新令牌
将“更新令牌放入”
令牌=会话['access_token']
刷新令牌=会话[“刷新令牌”]
expire_at=会话['expire_at']
@access\u-token=OAuth2::AccessToken.from\u-hash(获取\u客户端,{:access\u-token=>token,:refresh\u-token=>refresh\u-token,:expire\u-at=>expire\u-at,:header\u-format=>Bearer%s})
如果@access\u token.expired?
放置“刷新令牌”
@access\u token=@access\u token.refresh!
会话['access\u token']=@access\u token.token
会话['refresh\u token']=@access\u token.refresh\u token
会话['expire\u at']=@access\u token.expire\u at
会话['instance\u url']=@access\u token.params['instance\u url']
结束
结束
#向webservice发送post请求以发送令牌并创建post请求
def use_令牌
#我们得到了令牌,现在它将在标题中发布到web服务
#您还可以指定其他标题
#默认情况下包含令牌
更新令牌
conn=Faraday.new(:url=>'https://btadvertisingplatform.azurewebsites.net/|法拉第|
请求:url#u encoded#form encode POST参数
faraday.response:logger#记录对标准输出的请求
faraday.adapter faraday.default#适配器#使用Net::HTTP发出请求
结束
响应=conn.get do | req|
req.url'/api/WorkItem'
请求标题['Content-Type']='application/json'
请求标头['Authorization']='Bearer'+@access\u token.token
结束
@out=response.body
结束
def get_客户端
client=OAuth2::client.new(客户机ID、客户机机密、站点=>权限、授权url=>授权url、令牌url=>令牌url)
客户
结束
结束
由于您已经拥有资源:身份验证
您可以使用以下功能
<%= button_to 'Login', authentication_index_path, method: :get %>
<%= button_to 'Login', authentication_index_path, method: :get %>
get 'authentication/login', to: 'authentication#index'