Git Product home page Git Product logo

google-oauth-go-sample's Introduction

Google OAuth Go Sample Project - Web application

This is a web application that demonstrates how to do Google Oauth to log-in an authenticate users.

Installation

Simply go get github.com/Skarlso/google-oauth-go-sample.

Setup

Google

In order for the Google Authentication to work, you'll need developer credentials which the this application gathers from a file in the root directory called creds.json. The structure of this file should be like this:

{
  "installed": {
    "client_id": "hash.apps.googleusercontent.com",
    "project_id": "random",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_secret": "secret",
    "redirect_uris": [
      "http://localhost"
    ]
  }
}

To obtain these credentials, please navigate to this site and follow the procedure to setup a new project: Google Developer Console.

Once you have a new project, you need to create the above credentials. Navigate to the Project Page Credentials section and create an Oauth Client ID. Select Desktop app and you should have your Client ID like the above JSON document.

Dependencies

To gather all the libraries this project uses, simply execute from the root: go get -v ./...

Running

To run it, simply build & run and navigate to http://127.0.0.1:9090/login, nothing else should be required.

go build
./oauth-sample

google-oauth-go-sample's People

Contributors

dependabot[bot] avatar skarlso avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

google-oauth-go-sample's Issues

Value "state" is nil in AuthHandler

I can't get the sample to work, the session value "state" gets lost between the loginHandler and AuthHandler function and becomes . Any idea on how I can get it to work?

Invalid session state: retrieved: %!s(<nil>); Param: +T/zPeUjyzhebjn2f/vvwlNNc1gD5SCU1eDt3rjyEEA=

Thanks in advance,
Arnaud

Login fails

I was testing this per your instructions to get familiar with Oauth.
I get it running with keys, I get the Google screen asking me to use an account.

After I select a Google account to login with, it tries to load 127.0.0.1:9090 but on the server side I see it crashes.

Log below:

[GIN-debug] GET    /                         --> github.com/Skarlso/google-oauth-go-sample/handlers.IndexHandler (6 handlers)
[GIN-debug] GET    /login                    --> github.com/Skarlso/google-oauth-go-sample/handlers.LoginHandler (6 handlers)
[GIN-debug] GET    /auth                     --> github.com/Skarlso/google-oauth-go-sample/handlers.AuthHandler (6 handlers)
[GIN-debug] GET    /battle/field             --> github.com/Skarlso/google-oauth-go-sample/handlers.FieldHandler (7 handlers)
[GIN-debug] Listening and serving HTTP on 127.0.0.1:9090
[sessions] ERROR! securecookie: the value is not valid
[GIN] 2018/04/03 - 14:29:47 | 200 |      3.0002ms |       127.0.0.1 | GET      /login
[GIN] 2018/04/03 - 14:29:47 | 200 |      3.0002ms |       127.0.0.1 | GET      /login
[GIN] 2018/04/03 - 14:29:47 | 304 |            0s |       127.0.0.1 | GET      /css/main.css
[GIN] 2018/04/03 - 14:29:47 | 304 |            0s |       127.0.0.1 | GET      /css/main.css
[GIN] 2018/04/03 - 14:29:47 | 304 |      1.0001ms |       127.0.0.1 | GET      /img/background.png
[GIN] 2018/04/03 - 14:29:47 | 304 |      1.0001ms |       127.0.0.1 | GET      /img/background.png
[GIN] 2018/04/03 - 14:29:47 | 200 |    117.0067ms |       127.0.0.1 | GET      /img/favicon.ico
[GIN] 2018/04/03 - 14:29:47 | 200 |    117.0067ms |       127.0.0.1 | GET      /img/favicon.ico
[GIN] 2018/04/03 - 14:42:44 | 200 |      2.0001ms |       127.0.0.1 | GET      /
[GIN] 2018/04/03 - 14:42:44 | 200 |      2.0001ms |       127.0.0.1 | GET      /
[GIN] 2018/04/03 - 14:42:45 | 304 |      1.0001ms |       127.0.0.1 | GET      /css/main.css
[GIN] 2018/04/03 - 14:42:45 | 304 |      1.0001ms |       127.0.0.1 | GET      /css/main.css
[GIN] 2018/04/03 - 14:42:45 | 304 |            0s |       127.0.0.1 | GET      /img/background.png
[GIN] 2018/04/03 - 14:42:45 | 304 |            0s |       127.0.0.1 | GET      /img/background.png
[GIN] 2018/04/03 - 14:43:05 | 200 |      4.0002ms |       127.0.0.1 | GET      /
[GIN] 2018/04/03 - 14:43:05 | 200 |      4.0002ms |       127.0.0.1 | GET      /
[GIN] 2018/04/03 - 14:43:05 | 200 |           1ms |       127.0.0.1 | GET      /css/main.css
[GIN] 2018/04/03 - 14:43:05 | 200 |           1ms |       127.0.0.1 | GET      /css/main.css
[GIN] 2018/04/03 - 14:43:05 | 200 |      7.0004ms |       127.0.0.1 | GET      /img/background.png
[GIN] 2018/04/03 - 14:43:05 | 200 |      7.0004ms |       127.0.0.1 | GET      /img/background.png
[GIN] 2018/04/03 - 14:43:05 | 200 |      1.0001ms |       127.0.0.1 | GET      /img/favicon.ico
[GIN] 2018/04/03 - 14:43:05 | 200 |      1.0001ms |       127.0.0.1 | GET      /img/favicon.ico


2018/04/03 14:43:41 [Recovery] 2018/04/03 - 14:43:41 panic recovered:
GET /auth?state=lK0qne7srVrdHaKOURD%2B80zckgT%2Byqnl7iDlClEoFsU%3D&code=4/AABLkwbxW26Erqn2q_U2P3N73CGJx9pqCjvjW7wKH7h5u8xGrwEERr834tXodl6cKOqzg5u65vMNiEMb7-SnPOs HTTP/1.1
Host: 127.0.0.1:9090
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Connection: keep-alive
Cookie: goquestsession=MTUyMjc4NzM4N3xEdi1CQkFFQ180SUFBUkFCRUFBQVNfLUNBQUVHYzNSeWFXNW5EQWNBQlhOMFlYUmxCbk4wY21sdVp3d3VBQ3hzU3pCeGJtVTNjM0pXY21SSVlVdFBWVkpFS3pnd2VtTnJaMVFyZVhGdWJEZHBSR3hEYkVWdlJuTlZQUT09fChpe5YMfQN2NmPuXU4fVgWob7HYtpOXW1mIzV1tU7fR
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36


no reachable servers
C:/Go/src/runtime/panic.go:505 (0x42b496)
        gopanic: reflectcall(nil, unsafe.Pointer(d.fn), deferArgs(d), uint32(d.siz), uint32(d.siz))
C:/Users/SStoytchev/go/src/github.com/Skarlso/google-oauth-go-sample/database/mongo.go:44 (0x714a85)
        (*MongoDBConnection).GetSession: panic(err)
C:/Users/SStoytchev/go/src/github.com/Skarlso/google-oauth-go-sample/database/mongo.go:30 (0x7146a7)
        MongoDBConnection.LoadUser: mdb.session = mdb.GetSession()
C:/Users/SStoytchev/go/src/github.com/Skarlso/google-oauth-go-sample/handlers/handlers.go:107 (0x96b43f)
        AuthHandler: if _, mongoErr := db.LoadUser(u.Email); mongoErr == nil {
C:/Users/SStoytchev/go/src/github.com/gin-gonic/gin/context.go:107 (0x950b39)
        (*Context).Next: c.handlers[c.index](c)
C:/Users/SStoytchev/go/src/github.com/gin-gonic/contrib/sessions/sessions.go:65 (0x963e54)
        Sessions.func1: c.Next()
C:/Users/SStoytchev/go/src/github.com/gin-gonic/gin/context.go:107 (0x950b39)
        (*Context).Next: c.handlers[c.index](c)
C:/Users/SStoytchev/go/src/github.com/gin-gonic/gin/recovery.go:47 (0x961440)
        RecoveryWithWriter.func1: c.Next()
C:/Users/SStoytchev/go/src/github.com/gin-gonic/gin/context.go:107 (0x950b39)
        (*Context).Next: c.handlers[c.index](c)
C:/Users/SStoytchev/go/src/github.com/gin-gonic/gin/logger.go:83 (0x9606e2)
        LoggerWithWriter.func1: c.Next()
C:/Users/SStoytchev/go/src/github.com/gin-gonic/gin/context.go:107 (0x950b39)
        (*Context).Next: c.handlers[c.index](c)
C:/Users/SStoytchev/go/src/github.com/gin-gonic/gin/recovery.go:47 (0x961440)
        RecoveryWithWriter.func1: c.Next()
C:/Users/SStoytchev/go/src/github.com/gin-gonic/gin/context.go:107 (0x950b39)
        (*Context).Next: c.handlers[c.index](c)
C:/Users/SStoytchev/go/src/github.com/gin-gonic/gin/logger.go:83 (0x9606e2)
        LoggerWithWriter.func1: c.Next()
C:/Users/SStoytchev/go/src/github.com/gin-gonic/gin/context.go:107 (0x950b39)
        (*Context).Next: c.handlers[c.index](c)
C:/Users/SStoytchev/go/src/github.com/gin-gonic/gin/gin.go:359 (0x95860c)
        (*Engine).handleHTTPRequest: c.Next()
C:/Users/SStoytchev/go/src/github.com/gin-gonic/gin/gin.go:326 (0x957e59)
        (*Engine).ServeHTTP: engine.handleHTTPRequest(c)
C:/Go/src/net/http/server.go:2694 (0x6833d2)
        serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
C:/Go/src/net/http/server.go:1830 (0x67f4c7)
        (*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
C:/Go/src/runtime/asm_amd64.s:2361 (0x456000)
        goexit: BYTE    $0x90   // NOP

I suspect this error "[sessions] ERROR! securecookie: the value is not valid" makes the session invalid and the database crashes trying to get the session? Any idea on how to fix it?

Cleanup

This code needs some cleanup.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.