55 "bytes"
66 "encoding/xml"
77 "html/template"
8- "log"
98 "net/http"
109 "net/http/cookiejar"
1110 "net/url"
@@ -90,25 +89,29 @@ func New(pUser, pPass, pEndpoint string) CookieAuth {
9089// Cookies creates a CookieResponse. It fetches the auth token and then
9190// retrieves the Cookies
9291func (ca * CookieAuth ) Cookies () (CookieResponse , error ) {
93- return ca .getSPCookie (ca .getSPToken ())
92+ spToken , err := ca .getSPToken ()
93+ if err != nil {
94+ return CookieResponse {}, err
95+ }
96+ return ca .getSPCookie (spToken )
9497}
9598
9699func (ca * CookieAuth ) getSPCookie (conf * SuccessResponse ) (CookieResponse , error ) {
97100 spRoot , err := url .Parse (ca .endpoint )
98101 if err != nil {
99- panic ( err )
102+ return CookieResponse {}, err
100103 }
101104
102105 u , err := url .Parse ("https://" + spRoot .Host + "/_forms/default.aspx?wa=wsignin1.0" )
103106 if err != nil {
104- log . Fatal ( err )
107+ return CookieResponse {}, err
105108 }
106109
107110 // To authenticate with davfs or anything else we need two cookies (rtFa and FedAuth)
108111 // In order to get them we use the token we got earlier and a cookieJar
109112 jar , err := cookiejar .New (& cookiejar.Options {PublicSuffixList : publicsuffix .List })
110113 if err != nil {
111- log . Fatal ( err )
114+ return CookieResponse {}, err
112115 }
113116
114117 client := & http.Client {
@@ -117,7 +120,7 @@ func (ca *CookieAuth) getSPCookie(conf *SuccessResponse) (CookieResponse, error)
117120
118121 // Send the previously aquired Token as a Post parameter
119122 if _ , err = client .Post (u .String (), "text/xml" , strings .NewReader (conf .Succ .Token )); err != nil {
120- log . Fatal ( err )
123+ return CookieResponse {}, err
121124 }
122125
123126 cookieResponse := CookieResponse {}
@@ -134,7 +137,7 @@ func (ca *CookieAuth) getSPCookie(conf *SuccessResponse) (CookieResponse, error)
134137 return cookieResponse , err
135138}
136139
137- func (ca * CookieAuth ) getSPToken () * SuccessResponse {
140+ func (ca * CookieAuth ) getSPToken () ( * SuccessResponse , error ) {
138141 reqData := map [string ]interface {}{
139142 "Username" : ca .user ,
140143 "Password" : ca .pass ,
@@ -145,20 +148,20 @@ func (ca *CookieAuth) getSPToken() *SuccessResponse {
145148
146149 buf := & bytes.Buffer {}
147150 if err := t .Execute (buf , reqData ); err != nil {
148- panic ( err )
151+ return nil , err
149152 }
150153
151154 // Execute the first request which gives us an auth token for the sharepoint service
152155 // With this token we can authenticate on the login page and save the returned cookies
153156 req , err := http .NewRequest ("POST" , "https://login.microsoftonline.com/extSTS.srf" , buf )
154157 if err != nil {
155- panic ( err )
158+ return nil , err
156159 }
157160
158161 client := & http.Client {}
159162 resp , err := client .Do (req )
160163 if err != nil {
161- panic ( err . Error ())
164+ return nil , err
162165 }
163166 defer resp .Body .Close ()
164167
@@ -169,8 +172,8 @@ func (ca *CookieAuth) getSPToken() *SuccessResponse {
169172 var conf SuccessResponse
170173 err = xml .Unmarshal (s , & conf )
171174 if err != nil {
172- panic ( err )
175+ return nil , err
173176 }
174177
175- return & conf
178+ return & conf , err
176179}
0 commit comments