Skip to content

Instantly share code, notes, and snippets.

@aesteve
Last active December 16, 2015 16:48
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 aesteve/dd771f51929c9a366839 to your computer and use it in GitHub Desktop.
Save aesteve/dd771f51929c9a366839 to your computer and use it in GitHub Desktop.
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
Copy link

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