12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
+ use std:: collections:: HashMap ;
16
+
15
17
use risingwave_pb:: user:: auth_info:: EncryptionType ;
16
18
use risingwave_pb:: user:: AuthInfo ;
19
+ use risingwave_sqlparser:: ast:: SqlOption ;
17
20
use sha2:: { Digest , Sha256 } ;
18
21
22
+ use crate :: WithOptions ;
23
+
19
24
// SHA-256 is not supported in PostgreSQL protocol. We need to implement SCRAM-SHA-256 instead
20
25
// if necessary.
21
26
const SHA256_ENCRYPTED_PREFIX : & str = "SHA-256:" ;
@@ -24,6 +29,27 @@ const MD5_ENCRYPTED_PREFIX: &str = "md5";
24
29
const VALID_SHA256_ENCRYPTED_LEN : usize = SHA256_ENCRYPTED_PREFIX . len ( ) + 64 ;
25
30
const VALID_MD5_ENCRYPTED_LEN : usize = MD5_ENCRYPTED_PREFIX . len ( ) + 32 ;
26
31
32
+ pub const OAUTH_JWKS_URL_KEY : & str = "jwks_url" ;
33
+ pub const OAUTH_ISSUER_KEY : & str = "issuer" ;
34
+
35
+ /// Build `AuthInfo` for `OAuth`.
36
+ #[ inline( always) ]
37
+ pub fn build_oauth_info ( options : & Vec < SqlOption > ) -> Option < AuthInfo > {
38
+ let metadata: HashMap < String , String > = WithOptions :: try_from ( options. as_slice ( ) )
39
+ . ok ( ) ?
40
+ . into_inner ( )
41
+ . into_iter ( )
42
+ . collect ( ) ;
43
+ if !metadata. contains_key ( OAUTH_JWKS_URL_KEY ) || !metadata. contains_key ( OAUTH_ISSUER_KEY ) {
44
+ return None ;
45
+ }
46
+ Some ( AuthInfo {
47
+ encryption_type : EncryptionType :: Oauth as i32 ,
48
+ encrypted_value : Vec :: new ( ) ,
49
+ metadata,
50
+ } )
51
+ }
52
+
27
53
/// Try to extract the encryption password from given password. The password is always stored
28
54
/// encrypted in the system catalogs. The ENCRYPTED keyword has no effect, but is accepted for
29
55
/// backwards compatibility. The method of encryption is by default SHA-256-encrypted. If the
@@ -53,11 +79,13 @@ pub fn encrypted_password(name: &str, password: &str) -> Option<AuthInfo> {
53
79
Some ( AuthInfo {
54
80
encryption_type : EncryptionType :: Sha256 as i32 ,
55
81
encrypted_value : password. trim_start_matches ( SHA256_ENCRYPTED_PREFIX ) . into ( ) ,
82
+ metadata : HashMap :: new ( ) ,
56
83
} )
57
84
} else if valid_md5_password ( password) {
58
85
Some ( AuthInfo {
59
86
encryption_type : EncryptionType :: Md5 as i32 ,
60
87
encrypted_value : password. trim_start_matches ( MD5_ENCRYPTED_PREFIX ) . into ( ) ,
88
+ metadata : HashMap :: new ( ) ,
61
89
} )
62
90
} else {
63
91
Some ( encrypt_default ( name, password) )
@@ -70,6 +98,7 @@ fn encrypt_default(name: &str, password: &str) -> AuthInfo {
70
98
AuthInfo {
71
99
encryption_type : EncryptionType :: Md5 as i32 ,
72
100
encrypted_value : md5_hash ( name, password) ,
101
+ metadata : HashMap :: new ( ) ,
73
102
}
74
103
}
75
104
@@ -81,6 +110,7 @@ pub fn encrypted_raw_password(info: &AuthInfo) -> String {
81
110
EncryptionType :: Plaintext => "" ,
82
111
EncryptionType :: Sha256 => SHA256_ENCRYPTED_PREFIX ,
83
112
EncryptionType :: Md5 => MD5_ENCRYPTED_PREFIX ,
113
+ EncryptionType :: Oauth => "" ,
84
114
} ;
85
115
format ! ( "{}{}" , prefix, encrypted_pwd)
86
116
}
@@ -156,15 +186,18 @@ mod tests {
156
186
Some ( AuthInfo {
157
187
encryption_type: EncryptionType :: Md5 as i32 ,
158
188
encrypted_value: md5_hash( user_name, password) ,
189
+ metadata: HashMap :: new( ) ,
159
190
} ) ,
160
191
None ,
161
192
Some ( AuthInfo {
162
193
encryption_type: EncryptionType :: Md5 as i32 ,
163
194
encrypted_value: md5_hash( user_name, password) ,
195
+ metadata: HashMap :: new( ) ,
164
196
} ) ,
165
197
Some ( AuthInfo {
166
198
encryption_type: EncryptionType :: Sha256 as i32 ,
167
199
encrypted_value: sha256_hash( user_name, password) ,
200
+ metadata: HashMap :: new( ) ,
168
201
} ) ,
169
202
] ;
170
203
let output_passwords = input_passwords
0 commit comments