File tree Expand file tree Collapse file tree 2 files changed +32
-6
lines changed Expand file tree Collapse file tree 2 files changed +32
-6
lines changed Original file line number Diff line number Diff line change 1
1
defmodule Mongo.UrlParser do
2
- @ moduledoc "Mongo connection URL parsing util"
2
+ @ moduledoc """
3
+ Mongo connection URL parsing util
4
+
5
+ [See](https://docs.mongodb.com/manual/reference/connection-string/#connections-connection-options) for the complete list of options.
6
+
7
+ """
3
8
4
9
@ mongo_url_regex ~r/ ^mongodb(?<srv>\+ srv)?:\/ \/ ((?<username>[^:]+):(?<password>[^@]+)@)?(?<seeds>[^\/ ]+)(\/ (?<database>[^?]+))?(\? (?<options>.*))?$/
5
10
6
- # https://docs.mongodb.com/manual/reference/connection-string/#connections-connection-options
11
+ #https://docs.mongodb.com/manual/reference/connection-string/#connections-connection-options
7
12
@ mongo_options % {
8
13
# Path options
9
14
"username" => :string ,
@@ -90,17 +95,22 @@ defmodule Mongo.UrlParser do
90
95
opts
91
96
92
97
value ->
93
- key =
94
- key
95
- |> Macro . underscore ( )
96
- |> String . to_atom ( )
98
+ key = key
99
+ |> Macro . underscore ( )
100
+ |> String . to_atom ( )
101
+
102
+ value = decode_password ( key , value )
97
103
98
104
Keyword . put ( opts , @ driver_option_map [ key ] || key , value )
99
105
end
100
106
end
101
107
102
108
defp add_option ( _other , acc ) , do: acc
103
109
110
+ defp decode_password ( :username , value ) , do: URI . decode_www_form ( value )
111
+ defp decode_password ( :password , value ) , do: URI . decode_www_form ( value )
112
+ defp decode_password ( _other , value ) , do: value
113
+
104
114
defp parse_query_options ( opts , % { "options" => options } ) when is_binary ( options ) do
105
115
options
106
116
|> String . split ( "&" )
Original file line number Diff line number Diff line change @@ -72,5 +72,21 @@ defmodule Mongo.UrlParserTest do
72
72
]
73
73
end
74
74
end
75
+
76
+ test "encoded password" do
77
+
78
+ real_username = "@:/skøl:@/"
79
+ real_password = "@æœ{}%e()}@"
80
+
81
+ encoded_username = URI . encode_www_form ( real_username )
82
+ encoded_password = URI . encode_www_form ( real_password )
83
+ url = "mongodb://#{ encoded_username } :#{ encoded_password } @mymongodbserver:27017/admin"
84
+ opts = UrlParser . parse_url ( url: url )
85
+ password = Keyword . get ( opts , :password )
86
+ username = Keyword . get ( opts , :username )
87
+ assert password == real_password
88
+ assert username == real_username
89
+ end
90
+
75
91
end
76
92
end
You can’t perform that action at this time.
0 commit comments