{"id":18928443,"url":"https://github.com/nathan-osman/api2go-auth","last_synced_at":"2026-03-15T20:30:17.712Z","repository":{"id":57607769,"uuid":"127834780","full_name":"nathan-osman/api2go-auth","owner":"nathan-osman","description":"Handler for authenticating requests to api2go resources","archived":false,"fork":false,"pushed_at":"2018-04-06T02:23:36.000Z","size":32,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-12-31T19:16:00.651Z","etag":null,"topics":["api","authentication","golang","json-api"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nathan-osman.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-04-03T01:44:19.000Z","updated_at":"2018-12-09T20:58:55.000Z","dependencies_parsed_at":"2022-08-30T07:42:54.298Z","dependency_job_id":null,"html_url":"https://github.com/nathan-osman/api2go-auth","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nathan-osman%2Fapi2go-auth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nathan-osman%2Fapi2go-auth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nathan-osman%2Fapi2go-auth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nathan-osman%2Fapi2go-auth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nathan-osman","download_url":"https://codeload.github.com/nathan-osman/api2go-auth/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239923690,"owners_count":19719176,"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":["api","authentication","golang","json-api"],"created_at":"2024-11-08T11:25:43.184Z","updated_at":"2026-03-15T20:30:17.678Z","avatar_url":"https://github.com/nathan-osman.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"## api2go-auth\n\n[![Build Status](https://travis-ci.org/nathan-osman/api2go-auth.svg?branch=master)](https://travis-ci.org/nathan-osman/api2go-auth)\n[![GoDoc](https://godoc.org/github.com/nathan-osman/api2go-auth?status.svg)](https://godoc.org/github.com/nathan-osman/api2go-auth)\n[![MIT License](http://img.shields.io/badge/license-MIT-9370d8.svg?style=flat)](http://opensource.org/licenses/MIT)\n\nThis package simplifies the task of adding authentication to an application using [api2go](https://github.com/manyminds/api2go).\n\n### Features\n\nHere are some of the features that api2go-auth provides:\n\n- Provides methods for login and logout\n- Ensures all API methods are authenticated\n- Enables full customization of the authentication process\n\n### Server Usage\n\nTo use api2go-auth, you must first create a type that implements [`Authenticator`](https://godoc.org/github.com/nathan-osman/api2go-auth#Authenticator). In the following example, user credentials are stored in a database:\n\n```go\ntype UserAuth struct {}\n\nfunc (u *UserAuth) Authenticate(r *http.Request) (interface{}, interface{}, error) {\n    u, err := isValidUser(r)\n    if err != nil {\n        return nil, nil, err\n    }\n    return u.ID, u, err\n}\n\nfunc (u *UserAuth) Initialize(r *http.Request, i interface{}) (*http.Request, error) {\n    u, err := fetchUser(i)\n    if err != nil {\n        return nil, err\n    }\n    return r.WithContext(\n        context.WithValue(r.Context(), \"user\", u)\n    ), nil\n}\n```\n\nThe `Authenticate()` method is invoked when the client attempts to login. Assuming valid credentials are supplied, the method returns both a unique identifier for the user as well as the user object itself (which will be sent to the client).\n\nThe `Initialize()` method is invoked before each API request. It loads the user object from the unique identifier (which was returned in `Authenticate()`) and adds a variable to the request context so that it can be used by data sources.\n\nThe next step is to simply create an [`Auth`](https://godoc.org/github.com/nathan-osman/api2go-auth#Auth) instance:\n\n```go\nvar (\n    api = api2go.NewAPI(\"api\")\n    h   = auth.New(api, \u0026UserAuth{}, nil)\n)\n```\n\n`h` can then be used as an HTTP handler.\n\n### Client Usage\n\nClients must log in my sending a POST request to the `/login` endpoint and including the data expected by `Authenticate` (a username and password, for example). If successful, the data returned by `Authenticate` will be send to the client in JSONAPI format. A cookie will be set that authenticates future requests.\n\nWhen a session is ready to be ended, the client may send a POST request to `/logout` to destroy the session.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnathan-osman%2Fapi2go-auth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnathan-osman%2Fapi2go-auth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnathan-osman%2Fapi2go-auth/lists"}