Example #1
0
func (ar *MariaDBAppRepo) CreateApplication(app *roll.Application) error {
	//Generate a client secret as needed
	if app.ClientSecret == "" {
		clientSecret, err := secrets.GenerateClientSecret()
		if err != nil {
			return err
		}
		app.ClientSecret = clientSecret
	}

	//Check JWT flow parts are ok
	if err := repos.CheckJWTCertParts(app); err != nil {
		return err
	}

	//Insert the app
	const appSql = `insert into rolldb.application(applicationName, clientId, clientSecret, developerEmail, developerId, loginProvider,
	redirectUri,jwtFlowAudience, jwtFlowIssuer, jwtFlowPublicKey) values(?,?,?,?,?,?,?,?,?,?)
	`
	stmt, err := ar.db.Prepare(appSql)
	if err != nil {
		return err
	}
	defer stmt.Close()

	_, err = stmt.Exec(
		app.ApplicationName,
		app.ClientID,
		app.ClientSecret,
		app.DeveloperEmail,
		app.DeveloperID,
		app.LoginProvider,
		app.RedirectURI,
		app.JWTFlowAudience,
		app.JWTFlowIssuer,
		app.JWTFlowPublicKey,
	)

	if err != nil {
		log.Info(err)
		sqlErr := err.(*mysql.MySQLError)
		switch sqlErr.Number {
		case 1062:
			log.Info("Duplicate app definition found")
			return repos.NewDuplicationAppdefError(app.ApplicationName, app.DeveloperEmail)
		default:
			return err
		}
	}

	return nil
}
Example #2
0
func applyUpdatesWithJWTColumns(db *sql.DB, app *roll.Application) error {
	//Check JWT flow parts are ok
	if err := repos.CheckJWTCertParts(app); err != nil {
		return err
	}

	const updateSql = `
	update application set loginProvider=?, redirectUri=?,jwtFlowPublicKey=?,jwtFlowIssuer=?,
	jwtFlowAudience=?,applicationName=? where clientId=?
	`
	stmt, err := db.Prepare(updateSql)
	if err != nil {
		return nil
	}

	defer stmt.Close()

	_, err = stmt.Exec(app.LoginProvider, app.RedirectURI, app.JWTFlowPublicKey, app.JWTFlowIssuer,
		app.JWTFlowAudience, app.ApplicationName, app.ClientID)
	return err

}