1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//! Library to valide google-sign-in tokens
//!
//! See https://developers.google.com/identity/sign-in/web/backend-auth
//!

#[macro_use]
extern crate serde_derive;
extern crate base64;
extern crate openssl;
extern crate serde;
extern crate serde_json;
extern crate time;

/// Error types and their utilities
pub mod errors;

use errors::Error;
use openssl::bn::BigNum;
use openssl::hash::MessageDigest;
use openssl::pkey::{PKey, Public};
use openssl::rsa::Rsa;
use openssl::sign::Verifier;
use std::collections::HashMap;
use std::io::Read;
use time::{Duration, OffsetDateTime};

type JsonValue = serde_json::value::Value;
type JsonObject = serde_json::map::Map<String, JsonValue>;

#[derive(Deserialize, Debug)]
struct Header {
    pub alg: String,
    pub kid: String,
}

struct Payload {
    pub sub: String,
    pub iss: String,
    pub aud: String,
    pub exp: OffsetDateTime,
}

#[derive(Deserialize)]
struct JsonKey {
    pub kty: String,
    pub alg: String,
    #[serde(rename = "use")]
    pub use_: String,
    pub kid: String,
    pub n: String,
    pub e: String,
}

#[derive(Deserialize)]
struct JsonKeys {
    pub keys: Vec<JsonKey>,
}

struct Key {
    alg: String,
    pkey: PKey<Public>,
}

type KeysMap = HashMap<String, Key>;

/// Context used to store Client_id and google public keys
pub struct Ctx {
    client_id: String,
    keys: KeysMap,
}

impl Ctx {
    /// Instantiate a new context
    ///
    /// Use `set_keys_from_reader` to set google public keys
    pub fn new(client_id: String) -> Ctx {
        Ctx {
            client_id: client_id,
            keys: HashMap::new(),
        }
    }

    fn set_keys_from_json_keys(&mut self, jsonkeys: JsonKeys) -> Result<(), Error> {
        let mut map: KeysMap = HashMap::new();

        for key in jsonkeys.keys {
            if key.use_ != "sig" {
                continue;
            }
            match key.alg.as_ref() {
                "RS256" => {
                    let n_decoded = base64_decode_url(&key.n)?;
                    let n = BigNum::from_slice(&n_decoded)?;
                    let e_decoded = base64_decode_url(&key.e)?;
                    let e = BigNum::from_slice(&e_decoded)?;

                    let rsa = Rsa::from_public_components(n, e)?;
                    let pkey: PKey<Public> = PKey::from_rsa(rsa)?;

                    let k = Key {
                        alg: key.alg.clone(),
                        pkey: pkey,
                    };
                    map.insert(key.kid, k);
                }
                _ => return Err(Error::UnsupportedAlgorithm),
            }
        }
        if map.len() == 0 {
            return Err(Error::NoKeys);
        }
        self.keys = map;
        Ok(())
    }

    /// Set google public keys used to verify tokens' signatures
    ///
    /// Expected format is JWK and an be found at
    /// https://www.googleapis.com/oauth2/v3/certs
    pub fn set_keys_from_reader<R>(&mut self, reader: R) -> Result<(), Error>
    where
        R: Read,
    {
        let jsonkeys: JsonKeys = serde_json::from_reader(reader)?;
        return self.set_keys_from_json_keys(jsonkeys);
    }

    /// Set google public keys used to verify tokens' signatures
    ///
    /// Expected format is JWK and an be found at
    /// https://www.googleapis.com/oauth2/v3/certs
    pub fn set_keys_from_str<'a>(&mut self, s: &'a str) -> Result<(), Error> {
        let jsonkeys: JsonKeys = serde_json::from_str(s)?;
        return self.set_keys_from_json_keys(jsonkeys);
    }

    /// Validate a google sign-in token
    ///
    /// What is checked:
    ///
    ///  -  The ID token is properly signed by Google using Google's public keys
    ///  -  The value of `aud` in the token is equal to the client ID.
    ///  -  The value of `iss` in the token is equal to accounts.google.com or
    ///     https://accounts.google.com.
    ///  -  The expiry time (exp) of the token has not passed, with an hour delay
    ///     to handle time skews
    ///
    /// Returns the `sub` field as a `String` or an `Error`
    pub fn google_signin_from_str(&self, token: &str) -> Result<String, Error> {
        let arr: Vec<&str> = token.split(".").collect();
        if arr.len() != 3 {
            return Err(Error::InvalidToken);
        }

        let hdr_base64 = arr[0];
        let payload_base64 = arr[1];
        let sig_base64 = arr[2];

        let hdr = decode_header(hdr_base64)?;
        let payload = decode_payload(payload_base64)?;
        let sig = base64_decode_url(sig_base64)?;
        let sig_slice: &[u8] = &sig;

        verify_payload(self, &payload)?;
        verify_signature(self, &hdr, &hdr_base64, &payload_base64, sig_slice)?;

        Ok(payload.sub)
    }
}

fn base64_decode_url(msg: &str) -> Result<Vec<u8>, base64::DecodeError> {
    base64::decode_config(msg, base64::URL_SAFE)
}

fn decode_header(base64_hdr: &str) -> Result<Header, Error> {
    let hdr = base64_decode_url(base64_hdr)?;
    let hdr: Header = serde_json::from_slice(&hdr)?;
    Ok(hdr)
}

fn json_get_str<'a>(obj: &'a JsonObject, name: &'static str) -> Result<&'a str, Error> {
    let o: Option<&JsonValue> = obj.get(name);
    if let Some(v) = o {
        if !v.is_string() {
            return Err(Error::InvalidTypeField(name));
        }
        return Ok(v.as_str().unwrap());
    } else {
        return Err(Error::MissingField(name));
    }
}

fn json_get_numeric_date(obj: &JsonObject, name: &'static str) -> Result<OffsetDateTime, Error> {
    let o: Option<&JsonValue> = obj.get(name);
    if let Some(v) = o {
        if !v.is_i64() {
            return Err(Error::InvalidTypeField(name));
        }
        let sec = v.as_i64().unwrap();
        return Ok(OffsetDateTime::from_unix_timestamp(sec));
    } else {
        return Err(Error::MissingField(name));
    }
}

fn decode_payload(base64_payload: &str) -> Result<Payload, Error> {
    let payload_json = base64_decode_url(base64_payload)?;
    let obj: JsonValue = serde_json::from_slice(&payload_json)?;
    if !obj.is_object() {
        return Err(Error::InvalidTypeField(""));
    }
    let obj = obj.as_object().unwrap();

    let sub = json_get_str(obj, "sub")?;
    let iss = json_get_str(obj, "iss")?;
    let aud = json_get_str(obj, "aud")?;
    let exp = json_get_numeric_date(obj, "exp")?;

    Ok(Payload {
        sub: sub.to_string(),
        iss: iss.to_string(),
        aud: aud.to_string(),
        exp: exp,
    })
}

fn verify_payload(ctx: &Ctx, payload: &Payload) -> Result<(), Error> {
    if payload.aud != ctx.client_id {
        return Err(Error::InvalidAudience);
    }
    if payload.iss != "accounts.google.com" && payload.iss != "https://accounts.google.com" {
        return Err(Error::InvalidIssuer);
    }
    let now = OffsetDateTime::now_utc();
    if payload.exp + Duration::hours(1) < now {
        return Err(Error::Expired);
    }
    Ok(())
}

fn verify_rs256(txt: &str, key: &Key, sig: &[u8]) -> Result<(), Error> {
    let digest = MessageDigest::sha256();

    let mut verifier = Verifier::new(digest, &key.pkey)?;
    verifier.update(txt.as_bytes())?;
    let res = verifier.verify(sig);

    match res {
        Ok(true) => Ok(()),
        Ok(false) => Err(Error::InvalidSignature),
        Err(_) => Err(Error::InvalidSignature),
    }
}

fn verify_signature(
    ctx: &Ctx,
    hdr: &Header,
    hdr_base64: &str,
    payload_base64: &str,
    sig: &[u8],
) -> Result<(), Error> {
    let txt = format!("{}.{}", hdr_base64, payload_base64);

    let key = ctx.keys.get(&hdr.kid);
    if key.is_none() {
        return Err(Error::NoMatchingSigningKey);
    }
    let key = key.unwrap();
    if key.alg != hdr.alg {
        return Err(Error::NoMatchingSigningKey);
    }

    match key.alg.as_ref() {
        "RS256" => verify_rs256(&txt, key, sig),
        _ => Err(Error::UnsupportedAlgorithm),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use std::fs::File;

    fn content_from_file(filename: &str) -> String {
        let mut file = File::open(filename).unwrap();
        let mut buf = String::new();
        assert!(file.read_to_string(&mut buf).is_ok());
        buf.pop(); // remove trailing \n
        buf
    }

    #[test]
    fn from_token_file() {
        let token = content_from_file("token");
        let client_id = content_from_file("client_id");
        let mut ctx = Ctx::new(client_id);

        let keys = File::open("google_keys.json").unwrap();
        assert!(ctx.set_keys_from_reader(keys).is_ok());
        let res = ctx.google_signin_from_str(&token);
        assert!(res.is_ok());
    }
}