| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.command.ddl; |
| 7 |
|
| 8 |
import org.h2.api.ErrorCode; |
| 9 |
import org.h2.command.CommandInterface; |
| 10 |
import org.h2.engine.Database; |
| 11 |
import org.h2.engine.RightOwner; |
| 12 |
import org.h2.engine.SessionLocal; |
| 13 |
import org.h2.engine.User; |
| 14 |
import org.h2.expression.Expression; |
| 15 |
import org.h2.message.DbException; |
| 16 |
import org.h2.security.SHA256; |
| 17 |
import org.h2.util.StringUtils; |
| 18 |
import org.h2.value.DataType; |
| 19 |
import org.h2.value.Value; |
| 20 |
|
| 21 |
/** |
| 22 |
* This class represents the statement |
| 23 |
* CREATE USER |
| 24 |
*/ |
| 25 |
public class CreateUser extends DefineCommand { |
| 26 |
|
| 27 |
private String userName; |
| 28 |
private boolean admin; |
| 29 |
private Expression password; |
| 30 |
private Expression salt; |
| 31 |
private Expression hash; |
| 32 |
private boolean ifNotExists; |
| 33 |
private String comment; |
| 34 |
|
| 35 |
public CreateUser(SessionLocal session) { |
| 36 |
super(session); |
| 37 |
} |
| 38 |
|
| 39 |
public void setIfNotExists(boolean ifNotExists) { |
| 40 |
this.ifNotExists = ifNotExists; |
| 41 |
} |
| 42 |
|
| 43 |
public void setUserName(String userName) { |
| 44 |
this.userName = userName; |
| 45 |
} |
| 46 |
|
| 47 |
public void setPassword(Expression password) { |
| 48 |
this.password = password; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Set the salt and hash for the given user. |
| 53 |
* |
| 54 |
* @param user the user |
| 55 |
* @param session the session |
| 56 |
* @param salt the salt |
| 57 |
* @param hash the hash |
| 58 |
*/ |
| 59 |
static void setSaltAndHash(User user, SessionLocal session, Expression salt, Expression hash) { |
| 60 |
user.setSaltAndHash(getByteArray(session, salt), getByteArray(session, hash)); |
| 61 |
} |
| 62 |
|
| 63 |
private static byte[] getByteArray(SessionLocal session, Expression e) { |
| 64 |
Value value = e.optimize(session).getValue(session); |
| 65 |
if (DataType.isBinaryStringType(value.getValueType())) { |
| 66 |
byte[] b = value.getBytes(); |
| 67 |
return b == null ? new byte[0] : b; |
| 68 |
} |
| 69 |
String s = value.getString(); |
| 70 |
return s == null ? new byte[0] : StringUtils.convertHexToBytes(s); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Set the password for the given user. |
| 75 |
* |
| 76 |
* @param user the user |
| 77 |
* @param session the session |
| 78 |
* @param password the password |
| 79 |
*/ |
| 80 |
static void setPassword(User user, SessionLocal session, Expression password) { |
| 81 |
String pwd = password.optimize(session).getValue(session).getString(); |
| 82 |
char[] passwordChars = pwd == null ? new char[0] : pwd.toCharArray(); |
| 83 |
byte[] userPasswordHash; |
| 84 |
String userName = user.getName(); |
| 85 |
if (userName.isEmpty() && passwordChars.length == 0) { |
| 86 |
userPasswordHash = new byte[0]; |
| 87 |
} else { |
| 88 |
userPasswordHash = SHA256.getKeyPasswordHash(userName, passwordChars); |
| 89 |
} |
| 90 |
user.setUserPasswordHash(userPasswordHash); |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
public long update() { |
| 95 |
session.getUser().checkAdmin(); |
| 96 |
Database db = getDatabase(); |
| 97 |
RightOwner rightOwner = db.findUserOrRole(userName); |
| 98 |
if (rightOwner != null) { |
| 99 |
if (rightOwner instanceof User) { |
| 100 |
if (ifNotExists) { |
| 101 |
return 0; |
| 102 |
} |
| 103 |
throw DbException.get(ErrorCode.USER_ALREADY_EXISTS_1, userName); |
| 104 |
} |
| 105 |
throw DbException.get(ErrorCode.ROLE_ALREADY_EXISTS_1, userName); |
| 106 |
} |
| 107 |
int id = getObjectId(); |
| 108 |
User user = new User(db, id, userName, false); |
| 109 |
user.setAdmin(admin); |
| 110 |
user.setComment(comment); |
| 111 |
if (hash != null && salt != null) { |
| 112 |
setSaltAndHash(user, session, salt, hash); |
| 113 |
} else if (password != null) { |
| 114 |
setPassword(user, session, password); |
| 115 |
} else { |
| 116 |
throw DbException.getInternalError(); |
| 117 |
} |
| 118 |
db.addDatabaseObject(session, user); |
| 119 |
return 0; |
| 120 |
} |
| 121 |
|
| 122 |
public void setSalt(Expression e) { |
| 123 |
salt = e; |
| 124 |
} |
| 125 |
|
| 126 |
public void setHash(Expression e) { |
| 127 |
hash = e; |
| 128 |
} |
| 129 |
|
| 130 |
public void setAdmin(boolean b) { |
| 131 |
admin = b; |
| 132 |
} |
| 133 |
|
| 134 |
public void setComment(String comment) { |
| 135 |
this.comment = comment; |
| 136 |
} |
| 137 |
|
| 138 |
@Override |
| 139 |
public int getType() { |
| 140 |
return CommandInterface.CREATE_USER; |
| 141 |
} |
| 142 |
|
| 143 |
} |