@@ -30,36 +30,6 @@ impl AccessToken {
30
30
Self ( raw. into ( ) )
31
31
}
32
32
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
-
63
33
/// Wrap the raw access token with the token prefix and a checksum.
64
34
///
65
35
/// This turns e.g. `ABC` into `cio_tp_ABC{checksum}`.
@@ -82,10 +52,30 @@ impl FromStr for AccessToken {
82
52
type Err = AccessTokenError ;
83
53
84
54
/// Parse a string into an access token.
85
- ///
86
- /// This is equivalent to `AccessToken::from_byte_str`.
87
55
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 ( ) ) )
89
79
}
90
80
}
91
81
@@ -141,42 +131,33 @@ mod tests {
141
131
}
142
132
143
133
#[ test]
144
- fn test_from_byte_str ( ) {
134
+ fn test_from_str ( ) {
145
135
let token = AccessToken :: generate ( ) . finalize ( ) ;
146
136
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 > ( ) ) ;
148
138
assert_eq ! ( token2. finalize( ) . expose_secret( ) , token) ;
149
139
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 > ( ) ) ;
152
142
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 ) ;
158
145
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 ) ;
164
148
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 ) ;
170
151
171
- let bytes = b "cio_tp_000000@0000000000000000000000000";
152
+ let str = "cio_tp_000000@0000000000000000000000000" ;
172
153
assert_err_eq ! (
173
- AccessToken :: from_byte_str ( bytes ) ,
154
+ str . parse :: < AccessToken > ( ) ,
174
155
AccessTokenError :: InvalidCharacter
175
156
) ;
176
157
177
- let bytes = b "cio_tp_00000000000000000000000000000000";
158
+ let str = "cio_tp_00000000000000000000000000000000" ;
178
159
assert_err_eq ! (
179
- AccessToken :: from_byte_str ( bytes ) ,
160
+ str . parse :: < AccessToken > ( ) ,
180
161
AccessTokenError :: InvalidChecksum {
181
162
claimed: '0' ,
182
163
actual: 'w' ,
0 commit comments