OAuth.C 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (C) 2012 Emweb bvba, Kessel-Lo, Belgium.
  3. *
  4. * See the LICENSE file for terms of use.
  5. */
  6. #include <Wt/WApplication>
  7. #include <Wt/WContainerWidget>
  8. #include <Wt/WImage>
  9. #include <Wt/WServer>
  10. #include <Wt/WText>
  11. #include <Wt/Auth/AuthService>
  12. #include <Wt/Auth/GoogleService>
  13. Wt::Auth::AuthService authService;
  14. Wt::Auth::GoogleService *googleService = 0;
  15. class OAuthApplication : public Wt::WApplication
  16. {
  17. public:
  18. OAuthApplication(const Wt::WEnvironment& env)
  19. : Wt::WApplication(env)
  20. {
  21. if (!googleService) {
  22. new Wt::WText("This example requires a Google Auth service "
  23. "configuration", root());
  24. return;
  25. }
  26. process_ = googleService->createProcess
  27. (googleService->authenticationScope());
  28. Wt::WImage *ggi = new Wt::WImage("css/oauth-google.png", root());
  29. ggi->clicked().connect(process_,
  30. &Wt::Auth::OAuthProcess::startAuthenticate);
  31. process_->authenticated().connect(this, &OAuthApplication::authenticated);
  32. }
  33. void authenticated(const Wt::Auth::Identity& identity) {
  34. root()->clear();
  35. new Wt::WText("Welcome, " + identity.name(), root());
  36. }
  37. private:
  38. Wt::Auth::OAuthProcess* process_;
  39. };
  40. Wt::WApplication *createApplication(const Wt::WEnvironment& env)
  41. {
  42. return new OAuthApplication(env);
  43. }
  44. int main(int argc, char **argv)
  45. {
  46. try {
  47. Wt::WServer server(argc, argv, WTHTTP_CONFIGURATION);
  48. server.addEntryPoint(Wt::Application, createApplication);
  49. if (Wt::Auth::GoogleService::configured()) {
  50. googleService = new Wt::Auth::GoogleService(authService);
  51. }
  52. server.run();
  53. } catch (Wt::WServer::Exception& e) {
  54. std::cerr << e.what() << std::endl;
  55. } catch (std::exception &e) {
  56. std::cerr << "exception: " << e.what() << std::endl;
  57. }
  58. }