Skip to content

Commit

Permalink
Merge pull request #8 from cedarcode/credential_creation_options
Browse files Browse the repository at this point in the history
feat: offer credential_creation_options to the user for registration …
  • Loading branch information
grzuy committed May 22, 2018
2 parents 04a424c + 16d8ac1 commit 088cbd5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,26 @@ Or install it yourself as:

## Usage

### Registration

#### Initiation phase

```ruby
payload = WebAuthn.registration_payload
credential_creation_options = WebAuthn.credential_creation_options

# If you want to store your challenge, you can read it like this
payload[:publicKey][:challenge]
# Store the newly generated challenge somewhere so you can have it
# for the validation phase.
#
# You can read it from the resulting options:
credential_creation_options[:challenge]

render json: payload
# Send `credential_creation_options` to the browser, so that they can be used
# when calling `navigator.credentials.create({ "publicKey": credentialCreationOptions })`
```

#### Validation phase

TBD

## Development

Expand Down
12 changes: 5 additions & 7 deletions lib/webauthn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ module WebAuthn
USER_NAME = "web-user".freeze
CREATE_TYPE = "webauthn.create"

def self.registration_payload
def self.credential_creation_options
{
publicKey: {
challenge: Base64.urlsafe_encode64(SecureRandom.random_bytes(16)),
pubKeyCredParams: [ES256_ALGORITHM],
rp: { name: RP_NAME },
user: { name: USER_NAME, displayName: USER_NAME, id: Base64.urlsafe_encode64(USER_ID) }
}
challenge: Base64.urlsafe_encode64(SecureRandom.random_bytes(16)),
pubKeyCredParams: [ES256_ALGORITHM],
rp: { name: RP_NAME },
user: { name: USER_NAME, displayName: USER_NAME, id: Base64.urlsafe_encode64(USER_ID) }
}
end

Expand Down
14 changes: 7 additions & 7 deletions spec/webauthn_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@
expect(WebAuthn::VERSION).not_to be nil
end

describe "#registration_payload" do
describe "#credential_creation_options" do
before do
@payload = WebAuthn.registration_payload
@credential_creation_options = WebAuthn.credential_creation_options
end

it "has a 16 byte length challenge" do
original_challenge = Base64.urlsafe_decode64(@payload[:publicKey][:challenge])
original_challenge = Base64.urlsafe_decode64(@credential_creation_options[:challenge])
expect(original_challenge.length).to eq(16)
end

it "has public key params" do
expect(@payload[:publicKey][:pubKeyCredParams][0][:type]).to eq("public-key")
expect(@payload[:publicKey][:pubKeyCredParams][0][:alg]).to eq(-7)
expect(@credential_creation_options[:pubKeyCredParams][0][:type]).to eq("public-key")
expect(@credential_creation_options[:pubKeyCredParams][0][:alg]).to eq(-7)
end

it "has relying party info" do
expect(@payload[:publicKey][:rp][:name]).to eq("web-server")
expect(@credential_creation_options[:rp][:name]).to eq("web-server")
end

it "has user info" do
user_info = @payload[:publicKey][:user]
user_info = @credential_creation_options[:user]
expect(user_info[:name]).to eq("web-user")
expect(user_info[:displayName]).to eq("web-user")
expect(user_info[:id]).to eq("MQ==")
Expand Down

0 comments on commit 088cbd5

Please sign in to comment.