Skip to content

Instantly share code, notes, and snippets.

@fiorix
Created April 30, 2014 01:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fiorix/dc241189cef2ab7d6b12 to your computer and use it in GitHub Desktop.
Save fiorix/dc241189cef2ab7d6b12 to your computer and use it in GitHub Desktop.
Authenticate on Microsoft Exchange via the Outlook Web App
// Copyright 2014 Alexandre Fiori. All rights reserved.
// Use of this source code is governed by a BSD-style license.
//
// Auhenticate on Microsoft Exchange via the Outlook Web App.
package main
import (
"fmt"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
)
func owaPost(domain, username, password string) (*http.Response, error) {
baseURL := fmt.Sprintf("https://%s/owa/", domain)
form := url.Values{
"destination": {baseURL},
"username": {username},
"password": {password},
"flags": {"0"},
"forcedownlevel": {"0"},
"trusted": {"0"},
"isUtf8": {"1"},
}
req, err := http.NewRequest(
"POST",
baseURL+"auth/owaauth.dll",
strings.NewReader(form.Encode()),
)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
jar, _ := cookiejar.New(nil)
client := &http.Client{Jar: jar}
return client.Do(req)
}
func owaLogin(domain, username, password string) (bool, error) {
resp, err := owaPost(domain, username, password)
if err != nil {
return false, err
}
defer resp.Body.Close()
for _, cookie := range resp.Cookies() {
if cookie.Name == "UserContext" {
return true, nil
}
}
return false, nil
}
func main() {
ok, err := owaLogin("my-exchange.com", "username", "password")
if err != nil {
log.Fatal(err)
}
log.Println("Authenticated:", ok)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment