Skip to content

Instantly share code, notes, and snippets.

@michael-bey
Forked from staaldraad/oauthServer.go
Last active November 16, 2017 19:35
Show Gist options
  • Select an option

  • Save michael-bey/908e75dca8158652016e5b517df71fdb to your computer and use it in GitHub Desktop.

Select an option

Save michael-bey/908e75dca8158652016e5b517df71fdb to your computer and use it in GitHub Desktop.

Revisions

  1. michael-bey revised this gist Nov 16, 2017. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions oauthServer.go
    Original file line number Diff line number Diff line change
    @@ -31,8 +31,10 @@ func getToken(code string) {
    client_id := "fceae27c-cac4-4bd3-947e-xxxxxxx" //change to your APP-id
    scope := "offline_access%20people.read%20contacts.read.shared%20mail.read" //change to the permissions you need/want
    redirect_uri := "https%3A%2F%2Fxxx.xxx.xxx.xxx%2Fpermission" //change to match the Redirect URI you set in your app at apps.dev.microsoft.com
    client_secret := "encoded_pw_secret" //webapps now need a secret password, must be percent encoded

    postData := fmt.Sprintf("client_id=%s&scope=%s&code=%s&redirect_uri=%s&grant_type=authorization_code", client_id, scope, code, redirect_uri)

    postData := fmt.Sprintf("client_secret=%s&client_id=%s&scope=%s&code=%s&redirect_uri=%s&grant_type=authorization_code", client_secret, client_id, scope, code, redirect_uri)

    req, err := http.NewRequest("POST", "https://login.windows.net/common/oauth2/v2.0/token", strings.NewReader(postData))
    if err != nil {
    @@ -55,5 +57,5 @@ func getToken(code string) {
    func main() {
    fmt.Println("starting")
    http.HandleFunc("/", requestHandler)
    http.ListenAndServeTLS(":443", "cert.pem", "key.pem", nil)
    http.ListenAndServeTLS(":443", "/etc/letsencrypt/live/your-cert.pem", "/etc/letsencrypt/live/yourprivkey.pem", nil)
    }
  2. @staaldraad staaldraad renamed this gist Aug 2, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @staaldraad staaldraad created this gist Jul 7, 2017.
    59 changes: 59 additions & 0 deletions server.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    package main

    import (
    "crypto/tls"
    "fmt"
    "io/ioutil"
    "net/http"
    "net/url"
    "strings"
    )

    func requestHandler(w http.ResponseWriter, req *http.Request) {

    u, err := url.Parse(req.RequestURI)
    if err != nil {
    panic(err)
    }
    m, _ := url.ParseQuery(u.RawQuery)
    fmt.Println(u.RawQuery)
    getToken(m["code"][0])
    target := "https://outlook.office365.com/"
    //redirect the user so they think that everything was successful
    http.Redirect(w, req, target, http.StatusTemporaryRedirect)
    }

    func getToken(code string) {
    Transport := http.Transport{
    TLSClientConfig: &tls.Config{InsecureSkipVerify: false},
    }
    client := http.Client{Transport: &Transport}
    client_id := "fceae27c-cac4-4bd3-947e-xxxxxxx" //change to your APP-id
    scope := "offline_access%20people.read%20contacts.read.shared%20mail.read" //change to the permissions you need/want
    redirect_uri := "https%3A%2F%2Fxxx.xxx.xxx.xxx%2Fpermission" //change to match the Redirect URI you set in your app at apps.dev.microsoft.com

    postData := fmt.Sprintf("client_id=%s&scope=%s&code=%s&redirect_uri=%s&grant_type=authorization_code", client_id, scope, code, redirect_uri)

    req, err := http.NewRequest("POST", "https://login.windows.net/common/oauth2/v2.0/token", strings.NewReader(postData))
    if err != nil {
    return
    }
    req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
    resp, err := client.Do(req)
    if err != nil {
    panic(err)
    }
    defer resp.Body.Close()

    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
    return
    }
    fmt.Println(string(body))
    }

    func main() {
    fmt.Println("starting")
    http.HandleFunc("/", requestHandler)
    http.ListenAndServeTLS(":443", "cert.pem", "key.pem", nil)
    }