aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/debug_w_pmr/objects.mk
blob: dbda2a50b774509e38dc61f1c4e5b5a80d03769b (plain)
1
2
3
4
5
6
7
8
################################################################################
# Automatically-generated file. Do not edit!
################################################################################

USER_OBJS :=

LIBS := -llibc.a -l"C:/ti/TivaWare_C_Series-2.1.2.111/grlib/ccs/Debug/grlib.lib" -l"C:/ti/TivaWare_C_Series-2.1.2.111/driverlib/ccs/Debug/driverlib.lib" -l"C:/ti/TivaWare_C_Series-2.1.2.111/usblib/ccs/Debug/usblib.lib"
/ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Authentication;
using System.Text;
using System.Threading.Tasks;
using Tango.Transport.Web;

namespace Tango.Web.Security
{
    public class TokensManager<T> where T : IEquatable<T>
    {
        private Dictionary<String, TokenWrapper> _tokens;

        private class TokenWrapper : IEquatable<TokenWrapper>
        {
            public WebToken WebToken { get; set; }

            public T Value { get; set; }

            public bool Equals(TokenWrapper other)
            {
                return Value.Equals(other.Value);
            }
        }

        public TokensManager()
        {
            _tokens = new Dictionary<string, TokenWrapper>();
            ExpirationTime = TimeSpan.FromHours(1);
        }

        public TimeSpan ExpirationTime { get; set; }

        public WebToken GetOrCreate(T tokenObject)
        {
            var existing_token = _tokens.FirstOrDefault(x => x.Value.Value.Equals(tokenObject));

            if (existing_token.Key != null)
            {
                return existing_token.Value.WebToken;
            }
            else
            {
                String token = Guid.NewGuid().ToString();
                TokenWrapper wrapper = new TokenWrapper()
                {
                    Value = tokenObject,
                    WebToken = new WebToken()
                    {
                        //AccessToken = token,
                        //Expiration = DateTime.UtcNow.Add(ExpirationTime)
                    },
                };

                _tokens.Add(token, wrapper);
                return wrapper.WebToken;
            }
        }

        public void Validate(String token)
        {
            GetTokenObject(token);
        }

        public T GetTokenObject(String token)
        {
            if (!_tokens.ContainsKey(token))
            {
                throw new AuthenticationException("Invalid token.");
            }

            if (DateTime.UtcNow > _tokens[token].WebToken.Expiration)
            {
                _tokens.Remove(token);
                throw new TokenExpiredException("Session Expired.");
            }

            return _tokens[token].Value;
        }
    }
}