Connect with us

Tech

InvalidSegmentEncoding when trying to implement “Sign in with Google” using doorkeeper-grants_assertion

InvalidSegmentEncoding when trying to implement “Sign in with Google” using doorkeeper-grants_assertion

 


Sorry for the extremely long question but I wanted to provide more information in case it's important.

I'm trying to implement “Sign in with Google” in a Rails API application using Doorkeeper, but I'm stuck. I'm hoping I'm missing something obvious or there's a working example somewhere. I already have the endpoint api/v1/users/sign_in working for getting a token given a username and password. The controller action code for that is pretty simple:

def sign_in sign_in_with_strategy(“password”) end private def sign_in_with_strategy(strategy_name) strategy = server.token_request(strategy_name) auth = strategy.authorize if auth.is_a?(Doorkeeper::OAuth::ErrorResponse) return render_error(“Invalid credentials”, status: auth.status) end render json: { # Your code would include more data from auth.token, but that doesn't matter for the question data: auth.token.plaintext_token, } end

I have configured the following:

I created a project in the Google Cloud Console. Under credentials I have an OAuth 2.0 client ID, authorized JavaScript origin is http://localhost:3000, authorized redirect URIs are http://localhost:3000/api/v1/users/sign_in/3rd_party/redirect/google. For the OAuth consent screen, the user type is internal. Scopes are …/auth/userinfo.email, …/auth/userinfo.profile, openid. I added the doorkeeper-grants_assertion and omniauth-google-oauth2 gems. In config/initializers/doorkeeper.rb, I added the following code (slightly adapted from https://github.com/doorkeeper-gem/doorkeeper-grants_assertion/tree/9d185e44fb1245620ed2e50c0987f9628dc78995?tab=readme-ov-file#direct-omniauth-configuration, notably removing the rescue to see what part is failing): resource_owner_from_assertion do Rails.logger.info(“params: #{params}”) if params[:provider] && Parameter[:assertion]
# Devise (used by web app) and Doorkeeper (used by API) do not have the same provider. If this changes or you want to allow both, use https://github.com/doorkeeper-gem/doorkeeper-grants_assertion?tab=readme-ov-file#reuse-devise-configuration case params.fetch(:provider) when “google” auth0 = Doorkeeper::GrantsAssertion::OmniAuth.oauth2_wrapper( provider: :google, strategy_class: OmniAuth::Strategies::GoogleOauth2, client_id: ENV[“GOOGLE_CLIENT_ID”]client secret: ENV[“GOOGLE_CLIENT_SECRET”]client_options: { skip_image_info: true }, assertion: params.fetch(:assertion) ) auth = auth0.auth_hash Rails.logger.info(“inside resource_owner_from_assertion: auth0: #{auth0.inspect}; auth: #{auth.inspect}”) end if auth user = User.from_omniauth(auth) if user.present? && user.active_for_authentication? user end end end end and grant_flows updated to %w[password assertion]In the same controller that has the password sign-in action, I added these two: def sign_in_3rd_party end with the following view:

This page is for testing third-party sign-in to the API in your browser.

Click on any of the “Sign in” links below and you'll see a JSON response containing a bearer token.

<%= query = URI.encode_www_form( { client_id: ENV["GOOGLE_CLIENT_ID"], redirect_uri: url_for(action: :sign_in_3rd_party_redirect, provider: "google", only_path: false), response_type: "code", scope: "email" } ) link_to "Sign in with Google", "https://accounts.google.com/o/oauth2/v2/auth?#{query}" %>

def sign_in_3rd_party_redirect # Exchange the authorization code for an access token # https://developers.google.com/identity/protocols/oauth2/web-server#exchange-authorization-code response = HTTParty.post( “https://oauth2.googleapis.com/token”, headers: { “Content-Type” => “application/x-www-form-urlencoded” }, body: google_oauth2_params({ grant_type: “authorization_code”, code: params[:code]client secret: ENV[“GOOGLE_CLIENT_SECRET”]}), ) Response code == 200 Parameter[:assertion] = Response.ParsedResponse[“access_token”]
sign_in_with_strategy(“assertion”) else message = “Unexpected response from OAuth provider” + (Rails.env.development? ? “: #{response.parsed_response}” : “”) render_error(message, status: :bad_request) end end

When I visit the “Login with Google” link, I'm presented with the screen required to log in, then redirected to the sign_in_3rd_party_redirect action with the provider parameters (from the redirect URL) and the code in the query string. The exchange is also successful and I get an access token. However, I get the following error on the auth = auth0.auth_hash line in the resource_owner_from_assertion block:

4:00:55 AM web.1 | JWT::DecodeError – Invalid segment encoding: 4:00:55 AM web.1 | config/initializers/doorkeeper.rb:40:in `block (2 levels) in

' 4:00:55 AM web.1 | app/controllers/api/v1/users_session_controller.rb:194:in `sign_in_with_strategy' 4:00:55 AM web.1 | app/controllers/api/v1/users_session_controller.rb:54:in `sign_in_3rd_party_redirect' 4:00:55 AM web.1 | app/controllers/api/v1/api_controller.rb:34:in `switch_locale'

Am I doing something wrong? Or is there a bug in one of the libraries used? Versions:

Rails 7.0.8.4 Ruby 3.1.4 doorkeeper (5.6.6) doorkeeper-grants_assertion (0.3.1) omniauth (2.1.1) omniauth-google-oauth2 (1.1.2) omniauth-oauth2 (1.8.0) jwt (2.8.2)

Sources

1/ https://Google.com/

2/ https://stackoverflow.com/questions/78687455/invalidsegmentencoding-in-trying-to-implement-sign-in-with-google-with-doorkee

The mention sources can contact us to remove/changing this article

What Are The Main Benefits Of Comparing Car Insurance Quotes Online

LOS ANGELES, CA / ACCESSWIRE / June 24, 2020, / Compare-autoinsurance.Org has launched a new blog post that presents the main benefits of comparing multiple car insurance quotes. For more info and free online quotes, please visit https://compare-autoinsurance.Org/the-advantages-of-comparing-prices-with-car-insurance-quotes-online/ The modern society has numerous technological advantages. One important advantage is the speed at which information is sent and received. With the help of the internet, the shopping habits of many persons have drastically changed. The car insurance industry hasn't remained untouched by these changes. On the internet, drivers can compare insurance prices and find out which sellers have the best offers. View photos The advantages of comparing online car insurance quotes are the following: Online quotes can be obtained from anywhere and at any time. Unlike physical insurance agencies, websites don't have a specific schedule and they are available at any time. Drivers that have busy working schedules, can compare quotes from anywhere and at any time, even at midnight. Multiple choices. Almost all insurance providers, no matter if they are well-known brands or just local insurers, have an online presence. Online quotes will allow policyholders the chance to discover multiple insurance companies and check their prices. Drivers are no longer required to get quotes from just a few known insurance companies. Also, local and regional insurers can provide lower insurance rates for the same services. Accurate insurance estimates. Online quotes can only be accurate if the customers provide accurate and real info about their car models and driving history. Lying about past driving incidents can make the price estimates to be lower, but when dealing with an insurance company lying to them is useless. Usually, insurance companies will do research about a potential customer before granting him coverage. Online quotes can be sorted easily. Although drivers are recommended to not choose a policy just based on its price, drivers can easily sort quotes by insurance price. Using brokerage websites will allow drivers to get quotes from multiple insurers, thus making the comparison faster and easier. For additional info, money-saving tips, and free car insurance quotes, visit https://compare-autoinsurance.Org/ Compare-autoinsurance.Org is an online provider of life, home, health, and auto insurance quotes. This website is unique because it does not simply stick to one kind of insurance provider, but brings the clients the best deals from many different online insurance carriers. In this way, clients have access to offers from multiple carriers all in one place: this website. On this site, customers have access to quotes for insurance plans from various agencies, such as local or nationwide agencies, brand names insurance companies, etc. "Online quotes can easily help drivers obtain better car insurance deals. All they have to do is to complete an online form with accurate and real info, then compare prices", said Russell Rabichev, Marketing Director of Internet Marketing Company. CONTACT: Company Name: Internet Marketing CompanyPerson for contact Name: Gurgu CPhone Number: (818) 359-3898Email: [email protected]: https://compare-autoinsurance.Org/ SOURCE: Compare-autoinsurance.Org View source version on accesswire.Com:https://www.Accesswire.Com/595055/What-Are-The-Main-Benefits-Of-Comparing-Car-Insurance-Quotes-Online View photos

ExBUlletin

to request, modification Contact us at Here or [email protected]