/*
 * Migrate Oauth2 clients and Tokens to the new Authlib format
 * (mostly readability migration)
 */

const TOKEN_EXPIRATION = 30 * 24 * 60 * 60;  // 30 days in seconds

var nbClients = 0;
db.oauth2_client.find({}).forEach(client => {
    client.scopes = client.default_scopes;
    client.confidential = client.type === 'confidential';
    delete client.default_scopes;
    delete client.grant_type;
    delete client.profile;
    delete client.type;
    nbClients++;
})
print(`Updated ${nbClients} OAuth2 clients`);

var nbTokens = 0
db.oauth2_token.find().forEach(token => {
    token.create_at = new Date().setTime(token.expires.getTime() - TOKEN_EXPIRATION);
    token.expires_in = token.expires.getTime() - token.create_at.getTime();
    delete token.expires;
    db.oauth2_token.save(token);
    nbTokens++;
});
print(`Updated ${nbTokens} OAuth2 token(s)`);

@@ -227,24 +227,61 @@
 client =%3E %7B%0A
+    if (client.default_scopes) %7B%0A    
     client.s
@@ -307,24 +307,28 @@
 ult_scopes;%0A
+    
     client.c
@@ -373,24 +373,28 @@
 ntial';%0A    
+ 
+   
 delete clien
@@ -411,24 +411,28 @@
 scopes;%0A    
+   
+ 
 delete clien
@@ -441,24 +441,28 @@
 grant_type;%0A
+    
     delete c
@@ -476,24 +476,28 @@
 rofile;%0A    
+ 
+   
 delete clien
@@ -500,24 +500,67 @@
 lient.type;%0A
+        db.oauth2_client.save(client);%0A    
     nbClient
@@ -564,18 +564,25 @@
 ents++;%0A
+    %7D%0A
 %7D)
+;
 %0Aprint(%60
@@ -615,17 +615,19 @@
 2 client
-s
+(s)
 %60);%0A%0Avar
@@ -639,16 +639,17 @@
 kens = 0
+;
 %0Adb.oaut
@@ -662,16 +662,18 @@
 en.find(
+%7B%7D
 ).forEac
@@ -681,24 +681,53 @@
 (token =%3E %7B%0A
+    if (token.expires) %7B%0A    
     token.cr
@@ -730,16 +730,17 @@
 n.create
+d
 _at = ne
@@ -747,16 +747,42 @@
 w Date()
+;%0A        token.created_at
 .setTime
@@ -827,16 +827,20 @@
 ATION);%0A
+    
     toke
@@ -892,16 +892,17 @@
 n.create
+d
 _at.getT
@@ -904,24 +904,28 @@
 .getTime();%0A
+    
     delete t
@@ -938,24 +938,28 @@
 xpires;%0A    
+   
+ 
 db.oauth2_to
@@ -975,16 +975,20 @@
 token);%0A
+    
     nbTo
@@ -995,16 +995,22 @@
 kens++;%0A
+    %7D%0A
 %7D);%0Aprin

Fix authlib migration