{"id":19957854,"url":"https://github.com/andregarvin/oauth2","last_synced_at":"2026-05-12T18:31:35.137Z","repository":{"id":57547374,"uuid":"302478305","full_name":"andreGarvin/oauth2","owner":"andreGarvin","description":"This is a simple oauth to package I created in golang","archived":false,"fork":false,"pushed_at":"2023-07-10T19:03:30.000Z","size":10,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-12T07:14:08.615Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/andreGarvin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-10-08T22:46:21.000Z","updated_at":"2023-03-10T09:04:34.000Z","dependencies_parsed_at":"2024-06-20T00:19:01.871Z","dependency_job_id":null,"html_url":"https://github.com/andreGarvin/oauth2","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreGarvin%2Foauth2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreGarvin%2Foauth2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreGarvin%2Foauth2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreGarvin%2Foauth2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andreGarvin","download_url":"https://codeload.github.com/andreGarvin/oauth2/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241389167,"owners_count":19955107,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-13T01:39:06.435Z","updated_at":"2026-05-12T18:31:35.105Z","avatar_url":"https://github.com/andreGarvin.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# oauth2\n\nThis is a simple oauth2 package i made that anyone can use in there project that can help people add a SSO in there applications\n\n## Installation\n\nTo install the package, you need to install Go and set your Go workspace first.\n\n1. The first need [Go](https://golang.org/) installed (**version 1.13+**), then you can use the Go command below.\n\n```sh\n$ go get -u github.com/andreGarvin/oauth2\n```\n\n2. Import it in your code:\n\n```go\nimport \"github.com/andreGarvin/oauth2\"\n```\n\nThen you can start using it\n\n## Quick start\n\n```go\n// assuming the following codes in main.go file\n\npackage main\n\nimport (\n\t\"fmt\"\n\t\"log\"\n\t\"net/http\"\n\t\"oauth2\"\n)\n\nvar port = \":8080\"\n\nfunc main() {\n\n\tvar oauth = oauth2.New(\n\t\t\"\u003cclient id\u003e\",\n\t\t\"\u003coauth url\u003e\",\n\t\t\"\u003ctoken url\u003e\",\n\t\t\"http://localhost:8080/oauth/callback\",\n\t\t\"\u003cclient secret\u003e\",\n\t\toauth2.Scopes,\n\t)\n\n\t// endpoints\n\n\t// intitating a oauth login request and redirecting the user to the oauth service provider to authenticate the user on our service\n\thttp.HandleFunc(\"/oauth/signin\", func(w http.ResponseWriter, r *http.Request) {\n\t\tfmt.Println(\"signin: redirecting the user to foobarbaz.com oauth page\")\n\n\t\t// getting the formatted oauth url\n\t\t// or instead of using a string literal \"sigin\" use oauth2.OauthActionSignin, but this generally where state goes\n\t\toauthURL, err := oauth.CreateOauthURL(\"sigin\")\n\t\tif err != nil {\n\t\t\tfmt.Printf(\"error: %v\", err)\n\n\t\t\tw.WriteHeader(http.StatusInternalServerError)\n\n\t\t\tfmt.Fprint(w, \"internal server error\")\n\t\t\treturn\n\t\t}\n\n\t\tfmt.Println(oauthURL)\n\t\thttp.Redirect(w, r, oauthURL, http.StatusTemporaryRedirect)\n\t})\n\n\t// intitating a oauth login request and redirecting the user to the oauth service provider.\n\t// but going to set the state of the oauth url to be a oauth action to create a account on our service when it comes back to our service\n\thttp.HandleFunc(\"/oauth/create\", func(w http.ResponseWriter, r *http.Request) {\n\t\tfmt.Println(\"create: redirecting the user to foobarbaz.com oauth page\")\n\n\t\t// getting the formatted oauth url\n\t\toauthURL, err := oauth.CreateOauthURL(oauth2.OauthActionCreateAccount)\n\t\tif err != nil {\n\t\t\tfmt.Printf(\"error: %v\", err)\n\n\t\t\tw.WriteHeader(http.StatusInternalServerError)\n\n\t\t\tfmt.Fprint(w, \"internal server error\")\n\t\t\treturn\n\t\t}\n\n\t\thttp.Redirect(w, r, oauthURL, http.StatusTemporaryRedirect)\n\t})\n\n\t// and for OauthActionAuthorize you can have some authenticated endpoint that will performa action to your app to get access from the other app\n\n\t// handling oauth callback/redirect after a user has finished oauth against the IDP\n\thttp.HandleFunc(\"/oauth/callback\", func(w http.ResponseWriter, r *http.Request) {\n\t\tfmt.Println(\"handling the oauth callback\")\n\n\t\tquery := r.URL.Query()\n\n\t\t// this access code will allow to make a request from the service porvider to get the access token\n\t\taccessCode := query.Get(\"code\")\n\t\t// This would be the state information we sent in the beginning of the oauth request\n\t\tstate := query.Get(\"state\")\n\n\t\tuserOauthToken, err := oauth.FetchAccessToken(accessCode)\n\t\tif err != nil {\n\t\t\tfmt.Printf(\"failed to get user token information, %s\\n\", err)\n\n\t\t\tw.WriteHeader(http.StatusInternalServerError)\n\n\t\t\tfmt.Fprint(w, \"internal server error\")\n\t\t\treturn\n\t\t}\n\n\t\tfmt.Println(\"oauth user access Token\")\n\t\tfmt.Printf(\"\\n%+v\\n\", userOauthToken)\n\n\t\tswitch state {\n\t\tcase oauth2.OauthActionCreateAccount:\n\t\t\t// create a new user account\n\t\t\tbreak\n\t\tcase oauth2.OauthActionSignin:\n\t\t\t// authenticate a existing user\n\t\t\tbreak\n\t\tcase oauth2.OauthActionAuthorize:\n\t\t\t// add some authorization stuff for your application\n\t\t\tbreak\n\t\t}\n\n\t\tw.WriteHeader(http.StatusOK)\n\n\t\tfmt.Fprint(w, \"you have been authenticated\")\n\t})\n\n\tfmt.Printf(\"Running on Port %s\\n\", port)\n\tlog.Fatal(http.ListenAndServe(port, nil))\n}\n```\n\nAfter writing the code above code you can then run the command below and test it. After testing that it works you can customize whatever bit you want in the `/oauth/callback` for your application\n\n```\n# run example.go\n$ go run example.go\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandregarvin%2Foauth2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandregarvin%2Foauth2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandregarvin%2Foauth2/lists"}