Skip to content

Instantly share code, notes, and snippets.

@jonstorer
Created October 6, 2015 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonstorer/00984bda9d2c6dcbdf34 to your computer and use it in GitHub Desktop.
Save jonstorer/00984bda9d2c6dcbdf34 to your computer and use it in GitHub Desktop.
java all the things
public class ApplicationController {
private boolean ensure_valid_client() {
return current_oauth2_client.clientSecret == request.getClientSecret();
}
private OAuth2Client current_oauth2_client() {
return OAuth2Client oauth2Client = OAuth2Client.findById(request.getClientId());
}
}
public class AuthoziationController extends ApplicationController {
before_filter :ensure_valid_client
public void authorize() {
User user = User.findByLogin("jonstorer");
String route = null;
if (!user.passwordMatches("password")) {
route = "/failed?reason=user not found with that login and password";
} else {
// need to investigate associations in hibernate
AuthorizationCode authorizationCode = current_oauth2_client.authorization_codes.build({ user: user });
if (authorizationCode.save) {
route = "/whatever?code=" authorizationCode.code;
} else {
// choose JAVA throws or figure something else out
route = "/failed?reason=" authorizationCode.errors.map(&:full_message).join(' ');
}
}
redirect route;
}
}
public class User extends CrudRepository {
before_save :encrypt_password
// this might not be a thing
public User (Hash params) {
for (key : params) {
String methodName = "set" + key.substring(0,1).toUpper + key.substring(1);
this.getClass().getMethod(methodName, parms[key]).invoke(this, parms[key]);
}
this.salt = this.buildSalt();
return this;
}
public static User findByLogin (String login) {
// don't expose account type and user status to
// the controller. The controller doesn't need
// to know that.
return this.findByUserIDAndAccountTypeAndUserStatus(login, "A", "A");
}
public boolean passwordsMatch (String passphrase) {
return this.encrypt(passphrase) == this.password;
}
private String encrypt (String string) {
// encrypt with this.salt;
}
private String buildSalt () {
// gen code
}
}
public class AuthorizationCode extends SomeOrm {
belongs_to :oauth2Client;
belongs_to :user;
validates :requireCode
private void requireCode () {
return !!self.code;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment