@@ -30,36 +30,6 @@ impl AccessToken {
3030 Self ( raw. into ( ) )
3131 }
3232
33- /// Parse a byte string into an access token.
34- ///
35- /// This can be used to convert an HTTP header value into an access token.
36- pub fn from_byte_str ( byte_str : & [ u8 ] ) -> Result < Self , AccessTokenError > {
37- let suffix = byte_str
38- . strip_prefix ( Self :: PREFIX . as_bytes ( ) )
39- . ok_or ( AccessTokenError :: MissingPrefix ) ?;
40-
41- if suffix. len ( ) != Self :: RAW_LENGTH + 1 {
42- return Err ( AccessTokenError :: InvalidLength ) ;
43- }
44-
45- let suffix = std:: str:: from_utf8 ( suffix) . map_err ( |_| AccessTokenError :: InvalidCharacter ) ?;
46- if !suffix. chars ( ) . all ( |c| char:: is_ascii_alphanumeric ( & c) ) {
47- return Err ( AccessTokenError :: InvalidCharacter ) ;
48- }
49-
50- let raw = suffix. chars ( ) . take ( Self :: RAW_LENGTH ) . collect :: < String > ( ) ;
51- let claimed_checksum = suffix. chars ( ) . nth ( Self :: RAW_LENGTH ) . unwrap ( ) ;
52- let actual_checksum = checksum ( raw. as_bytes ( ) ) ;
53- if claimed_checksum != actual_checksum {
54- return Err ( AccessTokenError :: InvalidChecksum {
55- claimed : claimed_checksum,
56- actual : actual_checksum,
57- } ) ;
58- }
59-
60- Ok ( Self ( raw. into ( ) ) )
61- }
62-
6333 /// Wrap the raw access token with the token prefix and a checksum.
6434 ///
6535 /// This turns e.g. `ABC` into `cio_tp_ABC{checksum}`.
@@ -82,10 +52,30 @@ impl FromStr for AccessToken {
8252 type Err = AccessTokenError ;
8353
8454 /// Parse a string into an access token.
85- ///
86- /// This is equivalent to `AccessToken::from_byte_str`.
8755 fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
88- Self :: from_byte_str ( s. as_bytes ( ) )
56+ let suffix = s
57+ . strip_prefix ( Self :: PREFIX )
58+ . ok_or ( AccessTokenError :: MissingPrefix ) ?;
59+
60+ if suffix. len ( ) != Self :: RAW_LENGTH + 1 {
61+ return Err ( AccessTokenError :: InvalidLength ) ;
62+ }
63+
64+ if !suffix. chars ( ) . all ( |c| char:: is_ascii_alphanumeric ( & c) ) {
65+ return Err ( AccessTokenError :: InvalidCharacter ) ;
66+ }
67+
68+ let raw = suffix. chars ( ) . take ( Self :: RAW_LENGTH ) . collect :: < String > ( ) ;
69+ let claimed_checksum = suffix. chars ( ) . nth ( Self :: RAW_LENGTH ) . unwrap ( ) ;
70+ let actual_checksum = checksum ( raw. as_bytes ( ) ) ;
71+ if claimed_checksum != actual_checksum {
72+ return Err ( AccessTokenError :: InvalidChecksum {
73+ claimed : claimed_checksum,
74+ actual : actual_checksum,
75+ } ) ;
76+ }
77+
78+ Ok ( Self ( raw. into ( ) ) )
8979 }
9080}
9181
@@ -141,42 +131,33 @@ mod tests {
141131 }
142132
143133 #[ test]
144- fn test_from_byte_str ( ) {
134+ fn test_from_str ( ) {
145135 let token = AccessToken :: generate ( ) . finalize ( ) ;
146136 let token = token. expose_secret ( ) ;
147- let token2 = assert_ok ! ( AccessToken :: from_byte_str ( token . as_bytes ( ) ) ) ;
137+ let token2 = assert_ok ! ( token . parse :: < AccessToken > ( ) ) ;
148138 assert_eq ! ( token2. finalize( ) . expose_secret( ) , token) ;
149139
150- let bytes = b "cio_tp_0000000000000000000000000000000w";
151- assert_ok ! ( AccessToken :: from_byte_str ( bytes ) ) ;
140+ let str = "cio_tp_0000000000000000000000000000000w" ;
141+ assert_ok ! ( str . parse :: < AccessToken > ( ) ) ;
152142
153- let bytes = b"invalid_token" ;
154- assert_err_eq ! (
155- AccessToken :: from_byte_str( bytes) ,
156- AccessTokenError :: MissingPrefix
157- ) ;
143+ let str = "invalid_token" ;
144+ assert_err_eq ! ( str . parse:: <AccessToken >( ) , AccessTokenError :: MissingPrefix ) ;
158145
159- let bytes = b"cio_tp_invalid_token" ;
160- assert_err_eq ! (
161- AccessToken :: from_byte_str( bytes) ,
162- AccessTokenError :: InvalidLength
163- ) ;
146+ let str = "cio_tp_invalid_token" ;
147+ assert_err_eq ! ( str . parse:: <AccessToken >( ) , AccessTokenError :: InvalidLength ) ;
164148
165- let bytes = b"cio_tp_00000000000000000000000000" ;
166- assert_err_eq ! (
167- AccessToken :: from_byte_str( bytes) ,
168- AccessTokenError :: InvalidLength
169- ) ;
149+ let str = "cio_tp_00000000000000000000000000" ;
150+ assert_err_eq ! ( str . parse:: <AccessToken >( ) , AccessTokenError :: InvalidLength ) ;
170151
171- let bytes = b "cio_tp_000000@0000000000000000000000000";
152+ let str = "cio_tp_000000@0000000000000000000000000" ;
172153 assert_err_eq ! (
173- AccessToken :: from_byte_str ( bytes ) ,
154+ str . parse :: < AccessToken > ( ) ,
174155 AccessTokenError :: InvalidCharacter
175156 ) ;
176157
177- let bytes = b "cio_tp_00000000000000000000000000000000";
158+ let str = "cio_tp_00000000000000000000000000000000" ;
178159 assert_err_eq ! (
179- AccessToken :: from_byte_str ( bytes ) ,
160+ str . parse :: < AccessToken > ( ) ,
180161 AccessTokenError :: InvalidChecksum {
181162 claimed: '0' ,
182163 actual: 'w' ,
0 commit comments