|
| 1 | +# frozen_string_literal: true |
| 2 | +# rubocop:todo all |
| 3 | + |
| 4 | +# Copyright (C) 2014-2024 MongoDB, Inc. |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | + |
| 18 | +module Mongo |
| 19 | + module Auth |
| 20 | + |
| 21 | + # Defines behavior for OIDC authentication. |
| 22 | + # |
| 23 | + # @api private |
| 24 | + class Oidc < Base |
| 25 | + attr_reader :speculative_auth_result |
| 26 | + |
| 27 | + # The authentication mechanism string. |
| 28 | + # |
| 29 | + # @since 2.20.0 |
| 30 | + MECHANISM = 'MONGODB-OIDC'.freeze |
| 31 | + |
| 32 | + # Initializes the OIDC authenticator. |
| 33 | + # |
| 34 | + # @param [ Auth::User ] user The user to authenticate. |
| 35 | + # @param [ Mongo::Connection ] connection The connection to authenticate over. |
| 36 | + # |
| 37 | + # @option opts [ BSON::Document | nil ] speculative_auth_result The |
| 38 | + # value of speculativeAuthenticate field of hello response of |
| 39 | + # the handshake on the specified connection. |
| 40 | + def initialize(user, connection, **opts) |
| 41 | + super |
| 42 | + @speculative_auth_result = opts[:speculative_auth_result] |
| 43 | + @machine_workflow = MachineWorkflow::new(user.auth_mech_properties) |
| 44 | + end |
| 45 | + |
| 46 | + # Log the user in on the current connection. |
| 47 | + # |
| 48 | + # @return [ BSON::Document ] The document of the authentication response. |
| 49 | + def login |
| 50 | + execute_workflow(connection, conversation) |
| 51 | + end |
| 52 | + |
| 53 | + private |
| 54 | + |
| 55 | + def execute_workflow(connection, conversation) |
| 56 | + # If there is a cached access token, try to authenticate with it. If |
| 57 | + # authentication fails with an Authentication error (18), |
| 58 | + # invalidate the access token, fetch a new access token, and try |
| 59 | + # to authenticate again. |
| 60 | + # If the server fails for any other reason, do not clear the cache. |
| 61 | + if cache.access_token? |
| 62 | + token = cache.access_token |
| 63 | + msg = conversation.start(connection, token) |
| 64 | + begin |
| 65 | + dispatch_msg(connection, conversation, msg) |
| 66 | + rescue AuthError => error |
| 67 | + cache.invalidate(token) |
| 68 | + execute_workflow(connection, conversation) |
| 69 | + end |
| 70 | + end |
| 71 | + # This is the normal flow when no token is in the cache. Execute the |
| 72 | + # machine callback to get the token, put it in the caches, and then |
| 73 | + # send the saslStart to the server. |
| 74 | + token = machine_workflow.execute |
| 75 | + cache.access_token = token |
| 76 | + connection.access_token = token |
| 77 | + msg = conversation.start(connection, token) |
| 78 | + dispatch_msg(connection, conversation, msg) |
| 79 | + end |
| 80 | + end |
| 81 | + end |
| 82 | +end |
| 83 | + |
| 84 | +require 'mongo/auth/oidc/conversation' |
| 85 | +require 'mongo/auth/oidc/machine_workflow' |
| 86 | +require 'mongo/auth/oidc/token_cache' |
0 commit comments