miniature-sunset-64101
05/07/2023, 2:03 PMclass OryTestController < ActionController::Base
def index
# Set the configuration parameters
OryClient.configure do |configuration|
configuration.host = '<http://localhost:4000>'
configuration.debugging = true
end
# Initialize the client
ory_api = OryClient::FrontendApi.new
# Get the cookies
cookie_string = cookies.to_h.map{|k,v| "#{k}=#{v}"}.join('; ')
# Check the session
begin
session_response = ory_api.to_session({
cookie: cookie_string
})
rescue => e
session_response = JSON.parse(e.response_body)["error"]
end
session_response.deep_symbolize_keys!
if session_response[:status] == "Success" # @TODO: This should work, need to chat with ORY team about why it's not picking up the session
render json: session_response
else
redirect_to "<http://localhost:7777/sign-in>"
end
end
end
What am I doing wrong? I keep getting a 401 Unauthorized
response from the Kratos server, even though the session and cookies are valid?miniature-sunset-64101
05/07/2023, 2:05 PMminiature-sunset-64101
05/07/2023, 2:06 PMminiature-memory-51394
05/08/2023, 6:14 AM/sessions/whoami
directly from your frontend?miniature-sunset-64101
05/08/2023, 8:17 AMminiature-sunset-64101
05/08/2023, 8:18 AMminiature-sunset-64101
05/08/2023, 8:57 AMory_session_{{ project_slug }}
cookie value the same as the session_token
required in the SDK?miniature-memory-51394
05/08/2023, 9:51 AMminiature-sunset-64101
05/08/2023, 10:56 AMminiature-memory-51394
05/08/2023, 11:06 AMto_session
function.
please look at this example:
https://github.com/ory/sdk/blob/master/clients/client/ruby/docs/FrontendApi.md#to_session
hope it will help you 🙏
miniature-sunset-64101
05/08/2023, 11:34 AMminiature-sunset-64101
05/08/2023, 12:44 PMclass OryController < ActionController::Base
def index
# Set the configuration parameters
OryClient.configure do |config|
# Configure Bearer authorization: oryAccessToken
config.access_token = '{{ ory_pat_somthing_something }}'
end
# Initialize the client
ory_api = OryClient::IdentityApi.new
# Get the ory_session_id set by the front-end in the cookies
session_id = cookies[:ory_session_id]
# Check the session
begin
session_response = ory_api.get_session(session_id)
rescue => e
session_response = JSON.parse(e.response_body)["error"]
end
session_response.deep_symbolize_keys!
render json: session_response
end
end
However, now my response has gone from 401
to 403
, spot anything I may be missing?
Response is:
{
"code": 403,
"status": "Forbidden",
"request": "22ec332b-dcd2-9336-a358-b26166dee8d9",
"message": "Access credentials are not sufficient to access this resource"
}
magnificent-energy-493
miniature-sunset-64101
05/08/2023, 12:54 PMminiature-sunset-64101
05/08/2023, 12:54 PMminiature-sunset-64101
05/08/2023, 12:56 PMmagnificent-energy-493
There are several apps that will need to access the session, some use React, some use Ruby on Rails, and then we have a React-native mobile application as wellThis sounds like you could benefit from Ory Oathkeeper as an “identity gateway”
magnificent-energy-493
magnificent-energy-493
magnificent-energy-493
miniature-sunset-64101
05/08/2023, 2:03 PMAccess credentials are not sufficient to access this resource
Why are the credentials not sufficient if I provide an access_token
?miniature-sunset-64101
05/08/2023, 2:04 PMmagnificent-energy-493
miniature-sunset-64101
05/09/2023, 9:12 AMlocalhost:4000
(where the ORY tunnel is running), the Ruby SDK always uses the default playground.projects.oryapis.com host, which is why we kept getting the 403
response.
To fix this, they had to change the host in the gem to bypass the default. Perhaps this is some issue with the auto-generation?
What language is the original SDK written in? I believe it's Go?
The auto-generated SDKs might not all work the same way.
I'm still trying to check if there is a way of doing configuration that will override the defaults as intended without editing the gem, but a chance remains that there is a bug in the gem itself.magnificent-energy-493
const ory = new FrontendApi(
new Configuration({
basePath: basePath,
baseOptions: {
withCredentials: true,
},
})
);
There might be bugs/quirks in the Ruby SDK.
They are all autogenerated using OpenAPIGenerator from the swagger spec, so I believe there is no “original” SDK.
if there is a way of doing configuration that will override the defaults as intended without editing the gemThere definitely should be! I have no experience in Ruby, but feel free to open a bug report in github.com/ory/sdk if you can not figure it out 🙏
miniature-sunset-64101
05/09/2023, 9:38 AMhost
, only basePath
, which is sufficient to make it work.
In the Ruby SDK, there is both host
and base_path
and I'm not sure if they are the same thing, or 2 parts of a full URL, or if they are unrelated.
If we do find a bug and are sure about it, we'll log it on GitHub sure 🤘🖖magnificent-energy-493
miniature-sunset-64101
05/09/2023, 10:45 AMhost
doesn't quite work either
additionally my colleague believes he may have found the source of our problem. I'll ask him to describe it here for you. We suspect it may be an artefact from the auto-generationtall-application-76350
05/09/2023, 6:11 PMOryClient::FrontendApi
host the SDK will always revert to the default the default host, playground.projects.oryapis.com. The reason for this can be found in the base_url
getter method within the Ory Ruby SDK configuration.rb
def base_url(operation = nil)
index = server_operation_index.fetch(operation, server_index)
return "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '') if index == nil
server_url(index, server_operation_variables.fetch(operation, server_variables), operation_server_settings[operation])
end
which should early return <http://localhost:4000>
from the configured host
and scheme
, however the early return is skipped due to index
(in the above code snippet) returning the default server_index
value of 0 instead of nil (Which we believe should be the default value). Therefore, the fix is relatively simple: We just overrode the default server_index
value with nil.
Either this should be stated in the documentation if it was intended or the default changed to nil. Hope this helps 🙂