phabricator

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 24, 2023 License: MIT Imports: 6 Imported by: 1

README

go_oauth_phabricator

Client for OAuth2 Phabricator in Golang

Installation and Usage

Install

go get -v github.com/Megaputer/go_oauth_phabricator

Usage

package main

import (
	"fmt"
	"log"

	phabricator "github.com/Megaputer/go_oauth_phabricator"
)

var client *phabricator.Config

// initialize the client in the init () function
func init() {
	// Get oauthPHID and oauthSecret from
	// https://example.phabricator.com/oauthserver/query/all/
	oauthPHID := "OAuthPHID"
	oauthSecret := "OAuthSecret"

	// redirectURL is the URL to redirect users going through
	// the OAuth flow, after the resource owner's URLs.
	redirectURL := "https://my.com/auth"

	//phabricatorURL the url of the phabricator server
	// that is the source of OAuth
	phabricatorURL := "https://phabricator.example.com"

	client = phabricator.ClientConfig(oauthPHID, oauthSecret, redirectURL, phabricatorURL)
}

func main() {
	// AuthCodeURL return url from OAuth with CSRF token
	url := client.AuthCodeURL("CSRF token")
	fmt.Println(url)

	// code will be in the *http.Request.FormValue("code")
	// https://secure.phabricator.com/book/phabcontrib/article/using_oauthserver/
	code := ""

	user, err := client.Authenticate(code)
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Println(user.UserName, user.RealName)
}

Documentation

Overview

Package phabricator provides methods for using OAuth2 to access Phabricator.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// contains filtered or unexported fields
}

Config for OAuth

func ClientConfig

func ClientConfig(clientID string, сlientSecret string, redirectURL string, phabricatorURL string) *Config

ClientConfig сreates a pointer to the structure Config that is required to work with OAuth

clientID is the application's PHID https://example.phabricator.com/oauthserver/query/all/

clientSecret is the application's secret.

redirectURL is the URL to redirect users going through the OAuth flow, after the resource owner's URLs.

phabricatorURL the url of the phabricator server that is the source of OAuth

func (*Config) AuthCodeURL

func (c *Config) AuthCodeURL(state string) string

AuthCodeURL returns a URL to OAuth 2.0 provider's consent page that asks for permissions for the required scopes explicitly.

State is a token to protect the user from CSRF attacks. You must always provide a non-empty string and validate that it matches the the state query parameter on your redirect callback. See http://tools.ietf.org/html/rfc6749#section-10.12 for more info.

func (*Config) Authenticate

func (c *Config) Authenticate(code string) (User, error)

Authenticate returns the structure of the User, by code. The code will be in the *http.Request.FormValue("code") https://secure.phabricator.com/book/phabcontrib/article/using_oauthserver/

type User

type User struct {
	Phid         string   `json:"phid"`
	UserName     string   `json:"userName"`
	RealName     string   `json:"realName"`
	Image        string   `json:"image"`
	URI          string   `json:"uri"`
	Roles        []string `json:"roles"`
	PrimaryEmail string   `json:"primaryEmail"`
}

User is the result of the function JSON looks like:

{
 "phid": "PHID-USER-...",
 "userName": "...",
 "realName": "...",
 "image": phabricator_user_picture,
 "uri": phabricator_user_url,
 "roles": ["admin", "verified", "approved", "activated"],
 "primaryEmail": email
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL