• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 Google LLC
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *    * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *    * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *
15  *    * Neither the name of Google LLC nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 package com.google.auth.oauth2;
33 
34 import static com.google.common.base.Preconditions.checkNotNull;
35 
36 import com.google.api.client.http.HttpResponseException;
37 import com.google.api.client.json.GenericJson;
38 import com.google.api.client.json.JsonParser;
39 import java.io.IOException;
40 import javax.annotation.Nullable;
41 
42 /**
43  * Encapsulates the standard OAuth error response. See
44  * https://tools.ietf.org/html/rfc6749#section-5.2.
45  */
46 class OAuthException extends GoogleAuthException {
47 
48   private final String errorCode;
49   @Nullable private final String errorDescription;
50   @Nullable private final String errorUri;
51 
OAuthException(String errorCode, @Nullable String errorDescription, @Nullable String errorUri)52   OAuthException(String errorCode, @Nullable String errorDescription, @Nullable String errorUri) {
53     this.errorCode = checkNotNull(errorCode);
54     this.errorDescription = errorDescription;
55     this.errorUri = errorUri;
56   }
57 
58   @Override
getMessage()59   public String getMessage() {
60     // Fully specified message will have the format Error code %s: %s - %s.
61     StringBuilder sb = new StringBuilder("Error code " + errorCode);
62     if (errorDescription != null) {
63       sb.append(": ").append(errorDescription);
64     }
65     if (errorUri != null) {
66       sb.append(" - ").append(errorUri);
67     }
68     return sb.toString();
69   }
70 
getErrorCode()71   String getErrorCode() {
72     return errorCode;
73   }
74 
75   @Nullable
getErrorDescription()76   String getErrorDescription() {
77     return errorDescription;
78   }
79 
80   @Nullable
getErrorUri()81   String getErrorUri() {
82     return errorUri;
83   }
84 
createFromHttpResponseException(HttpResponseException e)85   static OAuthException createFromHttpResponseException(HttpResponseException e)
86       throws IOException {
87     JsonParser parser = OAuth2Utils.JSON_FACTORY.createJsonParser((e).getContent());
88     GenericJson errorResponse = parser.parseAndClose(GenericJson.class);
89 
90     String errorCode = (String) errorResponse.get("error");
91     String errorDescription = null;
92     String errorUri = null;
93     if (errorResponse.containsKey("error_description")) {
94       errorDescription = (String) errorResponse.get("error_description");
95     }
96     if (errorResponse.containsKey("error_uri")) {
97       errorUri = (String) errorResponse.get("error_uri");
98     }
99     return new OAuthException(errorCode, errorDescription, errorUri);
100   }
101 }
102