官术网_书友最值得收藏!

Getting started with OAuth2

OAuth2 is an open authorization standard designed to allow resource owners to give clients delegated access to private data (such as wall posts or tweets) via an access token exchange handshake. Even if you do not wish to access the private data, OAuth2 is a great option that allows people to sign in using their existing credentials, without exposing those credentials to a third-party site. In this case, we are the third party, and we want to allow our users to sign in using services that support OAuth2.

From a user's point of view, the OAuth2 flow is as follows:

  1. The user selects the provider with whom they wish to sign in to the client app.
  2. The user is redirected to the provider's website (with a URL that includes the client app ID) where they are asked to give permission to the client app.
  3. The user signs in from the OAuth2 service provider and accepts the permissions requested by the third-party application.
  4. The user is redirected to the client app with a request code.
  5. In the background, the client app sends the grant code to the provider, who sends back an authentication token.
  6. The client app uses the access token to make authorized requests to the provider, such as to get user information or wall posts.

To avoid reinventing the wheel, we will look at a few open source projects that have already solved this problem for us.

Open source OAuth2 packages

Andrew Gerrand has been working on the core Go team since February 2010, that is, two years before Go 1.0 was officially released. His goauth2 package (see https://github.com/golang/oauth2) is an elegant implementation of the OAuth2 protocol written entirely in Go.

Andrew's project inspired gomniauth (see https://github.com/stretchr/gomniauth). An open source Go alternative to Ruby's omniauth project, gomniauth provides a unified solution to access different OAuth2 services. In the future, when OAuth3 (or whatever the next-generation authorization protocol will be) comes out, in theory gomniauth could take on the pain of implementing the details, leaving the user code untouched.

For our application, we will use gomniauth to access OAuth services provided by Google, Facebook, and GitHub, so make sure you have it installed by running the following command:

go get github.com/stretchr/gomniauth

Tip

Some of the project dependencies of gomniauth are kept in Bazaar repositories, so you'll need to head over to http://wiki.bazaar.canonical.com to download them.

主站蜘蛛池模板: 怀远县| 辛集市| 中阳县| 历史| 云安县| 聂荣县| 云阳县| 溧水县| 祁门县| 江山市| 迁安市| 莆田市| 宁海县| 芮城县| 资中县| 土默特右旗| 阳新县| 乳源| 龙里县| 珠海市| 灵台县| 甘孜县| 凌云县| 平定县| 宁都县| 江陵县| 交口县| 郎溪县| 岳西县| 宣威市| 延长县| 襄樊市| 沈丘县| 车险| 正蓝旗| 宁海县| 衡阳县| 彩票| 环江| 新丰县| 沧州市|