Class: Aws::CognitoSrp
- Inherits:
-
Object
- Object
- Aws::CognitoSrp
- Defined in:
- lib/aws/cognito_srp.rb,
lib/aws/cognito_srp/errors.rb,
lib/aws/cognito_srp/version.rb
Overview
Client for AWS Cognito Identity Provider using Secure Remote Password (SRP).
Borrowed from: gist.github.com/jviney/5fd0fab96cd70d5d46853f052be4744c
This code is a direct translation of the Python version found here: github.com/capless/warrant/blob/ff2e4793d8479e770f2461ef7cbc0c15ee784395/warrant/aws_srp.py
Example usage:
aws_srp = Aws::CognitoSrp.new(
username: "username",
password: "password",
pool_id: "pool-id",
client_id: "client-id",
aws_client: Aws::CognitoIdentityProvider::Client.new(region: "us-west-2")
)
aws_srp.authenticate
Defined Under Namespace
Classes: Error, NewPasswordRequired, UnexpectedChallenge, ValueError
Constant Summary collapse
- NEW_PASSWORD_REQUIRED =
"NEW_PASSWORD_REQUIRED"
- PASSWORD_VERIFIER =
"PASSWORD_VERIFIER"
- REFRESH_TOKEN =
"REFRESH_TOKEN"
- USER_SRP_AUTH =
"USER_SRP_AUTH"
- N_HEX =
%w( FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1 29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245 E485B576 625E7EC6 F44C42E9 A637ED6B 0BFF5CB6 F406B7ED EE386BFB 5A899FA5 AE9F2411 7C4B1FE6 49286651 ECE45B3D C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8 FD24CF5F 83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D 670C354E 4ABC9804 F1746C08 CA18217C 32905E46 2E36CE3B E39E772C 180E8603 9B2783A2 EC07A28F B5C55DF0 6F4C52C9 DE2BCBF6 95581718 3995497C EA956AE5 15D22618 98FA0510 15728E5A 8AAAC42D AD33170D 04507A33 A85521AB DF1CBA64 ECFB8504 58DBEF0A 8AEA7157 5D060C7D B3970F85 A6E1E4C7 ABF5AE8C DB0933D7 1E8C94E0 4A25619D CEE3D226 1AD2EE6B F12FFA06 D98A0864 D8760273 3EC86A64 521F2B18 177B200C BBE11757 7A615D6C 770988C0 BAD946E2 08E24FA0 74E5AB31 43DB5BFC E0FD108E 4B82D120 A93AD2CA FFFFFFFF FFFFFFFF ).join.freeze
- G_HEX =
'2'
- INFO_BITS =
'Caldera Derived Key'
- VERSION =
"0.4.0"
Instance Method Summary collapse
- #authenticate ⇒ Object
-
#initialize(username:, password:, pool_id:, client_id:, aws_client:) ⇒ CognitoSrp
constructor
A new instance of CognitoSrp.
- #refresh_tokens(refresh_token) ⇒ Object (also: #refresh)
Constructor Details
#initialize(username:, password:, pool_id:, client_id:, aws_client:) ⇒ CognitoSrp
Returns a new instance of CognitoSrp.
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/aws/cognito_srp.rb', line 88 def initialize(username:, password:, pool_id:, client_id:, aws_client:) @username = username @password = password @pool_id = pool_id @client_id = client_id @aws_client = aws_client @big_n = hex_to_long(N_HEX) @g = hex_to_long(G_HEX) @k = hex_to_long(hex_hash("00#{N_HEX}0#{G_HEX}")) @small_a_value = generate_random_small_a @large_a_value = calculate_a end |
Instance Method Details
#authenticate ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/aws/cognito_srp.rb', line 102 def authenticate init_auth_response = @aws_client.initiate_auth( client_id: @client_id, auth_flow: USER_SRP_AUTH, auth_parameters: { USERNAME: @username, SRP_A: long_to_hex(@large_a_value) } ) unless init_auth_response.challenge_name == PASSWORD_VERIFIER raise UnexpectedChallenge, "Expected Cognito to respond with a #{PASSWORD_VERIFIER} challenge, got #{init_auth_response.challenge_name} instead" end challenge_response = process_challenge(init_auth_response.challenge_parameters) auth_response = @aws_client.respond_to_auth_challenge( client_id: @client_id, challenge_name: PASSWORD_VERIFIER, challenge_responses: challenge_response ) if auth_response.challenge_name == NEW_PASSWORD_REQUIRED raise NewPasswordRequired, "Cognito responded to password verifier with a #{NEW_PASSWORD_REQUIRED} challenge" end auth_response.authentication_result end |
#refresh_tokens(refresh_token) ⇒ Object Also known as: refresh
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/aws/cognito_srp.rb', line 131 def refresh_tokens(refresh_token) resp = @aws_client.initiate_auth( client_id: @client_id, auth_flow: REFRESH_TOKEN, auth_parameters: { REFRESH_TOKEN: refresh_token } ) resp.authentication_result end |