info "This script will create a new Oauth role and an associated database for Mattermost-LDAP\nTo edit configuration please edit this script before running !\n"
warn "SuperUser right must be ask to create the new role and database in mysql\n"
info "Press ctrl+c to stop the script"
sleep 5
#Creating Oauth role and associated database (need admin account on mysql)
info "Creation of role $oauth_user and database $oauth_db ... (need to be root)"
sudo mysql -u root --password=$mysql_pass --execute "CREATE DATABASE $oauth_db_name;"
sudo mysql -u root --password=$mysql_pass --execute "CREATE USER $oauth_user@'%' IDENTIFIED BY '$oauth_pass';"
sudo mysql -u root --password=$mysql_pass --execute "GRANT ALL PRIVILEGES ON $oauth_db_name.* TO $oauth_user@'%';"
#Creating tables for ouath database (use oauth role)
info "Creation of tables for database $oauth_db (using $oauth_user)"
mysql -u $oauth_user --password=$oauth_pass$oauth_db_name --execute "$create_table_oauth_client"
mysql -u $oauth_user --password=$oauth_pass$oauth_db_name --execute "$create_table_oauth_access_tokens"
mysql -u $oauth_user --password=$oauth_pass$oauth_db_name --execute "$create_table_oauth_authorization_codes"
mysql -u $oauth_user --password=$oauth_pass$oauth_db_name --execute "$create_table_oauth_refresh_tokens"
mysql -u $oauth_user --password=$oauth_pass$oauth_db_name --execute "$create_table_users"
mysql -u $oauth_user --password=$oauth_pass$oauth_db_name --execute "$create_table_oauth_scopes"
#Insert new client in the database
info "Insert new client in the database"
mysql -u $oauth_user --password=$oauth_pass$oauth_db_name --execute "$create_client"
#Verification
mysql -u $oauth_user --password=$oauth_pass$oauth_db_name --execute "SELECT * from oauth_clients WHERE client_id='$client_id';"| grep '(1'
if[$?]
then ok "Client has been created ! Oauth Database is configured.\n"
info "Client ID : $client_id"
warn "Client Secret : $client_secret\n"
info "Keep id and secret, you will need them to configure Mattermost"
warn "Beware Client Secret IS PRIVATE and MUST BE KEPT SECRET"
else error "Client has not been created ! Check log below"