Created
April 30, 2014 01:16
-
-
Save fiorix/dc241189cef2ab7d6b12 to your computer and use it in GitHub Desktop.
Authenticate on Microsoft Exchange via the Outlook Web App
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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