Skip to content

Instantly share code, notes, and snippets.

@aesteve

aesteve/GithubUser.groovy Secret

Last active Dec 16, 2015
Embed
What would you like to do?
setUser doesn't do anything
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.auth.AuthProvider;
import io.vertx.ext.auth.User
class GithubUser implements User {
String username
User original
public GithubUser(User original, String username) {
println "original : $original"
this.username = username
}
@Override
public User isAuthorised(String authority, Handler<AsyncResult<Boolean>> resultHandler) {
return original.isAuthorised(authority, resultHandler)
}
@Override
public User clearCache() {
return original.clearCache()
}
@Override
public JsonObject principal() {
return original.principal()
}
@Override
public void setAuthProvider(AuthProvider authProvider) {
original.setAuthProvider(authProvider)
}
}
Vertx vertx = Vertx.vertx()
Router router = Router.router(vertx)
Map options = [
clientID: '26e529e1a7637e236322',
clientSecret: 'b9c21eb9e330d9efc65156b7795eee30822d9e0b',
site: 'https://github.com/login',
tokenPath: '/oauth/access_token',
authorizationPath: '/oauth/authorize',
]
OAuth2Auth oauth = OAuth2Auth.create(vertx, OAuth2FlowType.AUTH_CODE, options)
OAuth2AuthHandler handler = OAuth2AuthHandler.create(oauth, 'http://localhost:9000')
handler.setupCallback(router.get('/login/github'))
router.get('/protected').handler handler
router.get('/protected').handler { ctx ->
if (ctx.user) {
println "change user"
User u = new User(new GithubUser(ctx.user.delegate, 'toto'))
println u.getDelegate()
ctx.user = u
}
ctx++
}
router.get('/protected').handler {
it.response.end "hello ${it.user}"
}
vertx.createHttpServer([host: 'localhost', port: 9000]).requestHandler(router.&accept).listen()
@pmlopes

This comment has been minimized.

Copy link

@pmlopes pmlopes commented Dec 16, 2015

package io.vertx.blog;

import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.auth.AuthProvider;
import io.vertx.ext.auth.User;

class GithubUser implements User {

  final User delegate;
  final String username;

  public GithubUser(User original, String username) {
    this.delegate = original;
    this.username = username;
  }

  @Override
  public User isAuthorised(String s, Handler<AsyncResult<Boolean>> handler) {
    return delegate.isAuthorised(s, handler);
  }

  @Override
  public User clearCache() {
    return delegate.clearCache();
  }

  @Override
  public JsonObject principal() {
    return delegate.principal();
  }

  @Override
  public void setAuthProvider(AuthProvider authProvider) {
    delegate.setAuthProvider(authProvider);
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.