@@ -51,13 +51,13 @@ defmodule NimbleTOTP do
5151 configured the authentication app in a compatible device. The most common
5252 way to do that is to generate a QR Code that can be read by the app.
5353
54- You can use `NimbleTOTP.otpauth_uri/3 ` along with
54+ You can use `NimbleTOTP.otpauth_uri/4 ` along with
5555 [eqrcode](https://github.com/SiliconJungles/eqrcode) to generate the QR
5656 code as **SVG**.
5757
5858 Example:
5959
60- uri = NimbleTOTP.otpauth_uri("Acme:alice ", secret, issuer: "Acme" )
60+ uri = NimbleTOTP.otpauth_uri("Acme", "alice", secret )
6161 #=> "otpauth://totp/Acme:alice?secret=MFRGGZA&issuer=Acme"
6262 uri |> EQRCode.encode() |> EQRCode.svg()
6363 #=> "<?xml version=\\" 1.0\\" standalone=\\" yes\\" ?>\\n<svg version=\\" 1.1\\" ...
@@ -143,20 +143,50 @@ defmodule NimbleTOTP do
143143 @ doc """
144144 Generate the URI to be encoded in the QR code.
145145
146+ ## Examples
147+
148+ iex> NimbleTOTP.otpauth_uri("Acme", "alice", "abcd")
149+ "otpauth://totp/Acme:alice?secret=MFRGGZA&issuer=Acme"
150+
151+ iex> NimbleTOTP.otpauth_uri("Acme", "alice", "abcd", extra: "some_value")
152+ "otpauth://totp/Acme:alice?secret=MFRGGZA&issuer=Acme&extra=some_value"
153+ """
154+ @ spec otpauth_uri ( String . t ( ) , String . t ( ) , << >> , keyword ( ) ) :: String . t ( )
155+ def otpauth_uri ( issuer , account , secret , uri_params )
156+ when is_binary ( issuer ) and is_binary ( account ) and is_binary ( secret ) and is_list ( uri_params ) do
157+ issuer =~ ":" && raise ArgumentError , "issuer cannot have :"
158+ account =~ ":" && raise ArgumentError , "account cannot have :"
159+ key = Base . encode32 ( secret , padding: false )
160+ params = uri_params |> Keyword . put ( :issuer , issuer ) |> Keyword . put ( :secret , key )
161+ query = URI . encode_query ( params , :rfc3986 )
162+ "otpauth://totp/#{ URI . encode ( issuer ) } :#{ URI . encode ( account ) } ?#{ query } "
163+ end
164+
165+ @ doc """
166+ Generate the URI to be encoded in the QR code.
167+
146168 ## Examples
147169
148170 iex> NimbleTOTP.otpauth_uri("Acme:alice", "abcd", issuer: "Acme")
149171 "otpauth://totp/Acme:alice?secret=MFRGGZA&issuer=Acme"
150172
151173 """
152- @ spec otpauth_uri ( String . t ( ) , String . t ( ) , keyword ( ) ) :: String . t ( )
153- def otpauth_uri ( label , secret , uri_params \\ [ ] ) when is_binary ( label ) and is_binary ( secret ) do
174+ @ spec otpauth_uri ( String . t ( ) , String . t ( ) , keyword ( ) | << >> ) :: String . t ( )
175+ def otpauth_uri ( label , secret , uri_params \\ [ ] )
176+
177+ def otpauth_uri ( label , secret , uri_params )
178+ when is_binary ( label ) and is_binary ( secret ) and is_list ( uri_params ) do
154179 key = Base . encode32 ( secret , padding: false )
155180 params = [ { :secret , key } | uri_params ]
156181 query = URI . encode_query ( params , :rfc3986 )
157182 "otpauth://totp/#{ URI . encode ( label ) } ?#{ query } "
158183 end
159184
185+ def otpauth_uri ( issuer , account , secret )
186+ when is_binary ( issuer ) and is_binary ( account ) and is_binary ( secret ) do
187+ otpauth_uri ( issuer , account , secret , [ ] )
188+ end
189+
160190 @ doc """
161191 Generate a binary composed of random bytes.
162192
0 commit comments