aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Web/Tango.MachineService/Security/TokenManager.cs
blob: abd6156860aa9f494ffd9eea7ca60156696f85d9 (plain)
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
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Authentication;
using System.Web;

namespace Tango.MachineService.Security
{
    public class TokenManager
    {
        private CloudTable GetTokensTable()
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(MachineServiceConfig.STORAGE_ACCOUNT);
            var client = storageAccount.CreateCloudTableClient();
            var table = client.GetTableReference("Tokens");
            table.CreateIfNotExists();
            return table;
        }

        public void AddToken(String token, String identity, DateTime expiration)
        {
            var table = GetTokensTable();

            table.Execute(TableOperation.InsertOrReplace(new TokenEntity()
            {
                PartitionKey = MachineServiceConfig.REFRESH_TOKENS_TABLE_PARTITION,
                RowKey = Guid.NewGuid().ToString(),
                AccessToken = token,
                Identity = identity,
                Expiration = expiration,
            }));
        }

        public void UpdateToken(String oldToken, String newToken, DateTime expiration)
        {
            var table = GetTokensTable();

            var existingToken = table.CreateQuery<TokenEntity>().AsQueryable().Where(x => x.AccessToken == oldToken).ToList().FirstOrDefault();

            if (existingToken == null)
            {
                throw new AuthenticationException("Invalid token.");
            }

            existingToken.AccessToken = newToken;
            existingToken.Expiration = expiration;

            table.Execute(TableOperation.InsertOrMerge(existingToken));
        }
    }
}
} private HardwareBlowerType _hardwareBlowerType; /// <summary> /// Gets or sets the type of the hardware Blower. /// </summary> [XmlIgnore] public HardwareBlowerType HardwareBlowerType { get { return _hardwareBlowerType; } set { _hardwareBlowerType = value; RaisePropertyChangedAuto(); TechName = _hardwareBlowerType != null ? _hardwareBlowerType.Description : null; ItemGuid = value != null ? value.Guid : null; if (_hardwareBlowerType != null) { HardwareBlower = BlowerConfigurations.SingleOrDefault(x => x.HardwareBlowerType == _hardwareBlowerType); } } } private HardwareBlower _hardwareBlower; /// <summary> /// Gets or sets the hardware Blower. /// </summary> [XmlIgnore] public HardwareBlower HardwareBlower { get { return _hardwareBlower; } set { _hardwareBlower = value; RaisePropertyChangedAuto(); } } private bool _isActive; [XmlIgnore] public bool IsActive { get { return _isActive; } set { _isActive = value; RaisePropertyChangedAuto(); } } private bool _effectiveActive; [XmlIgnore] public bool EffectiveActive { get { return _effectiveActive; } set { _effectiveActive = value; RaisePropertyChangedAuto(); IsActive = _effectiveActive; } } /// <summary> /// Gets or sets the set command. /// </summary> [XmlIgnore] public RelayCommand SetCommand { get; set; } [XmlIgnore] public RelayCommand ToggleActiveCommand { get; set; } /// <summary> /// Initializes a new instance of the <see cref="BlowerItem"/> class. /// </summary> public BlowerItem() : base() { Name = "Blower"; Description = "Blower Controller"; Image = ResourceHelper.GetImageFromResources("Images/blower.png"); Color = Colors.White; HardwareBlower = new HardwareBlower(); SetCommand = new RelayCommand(() => { SetCommandClicked?.Invoke(this, _isActive); }); ToggleActiveCommand = new RelayCommand(() => { SetCommandClicked?.Invoke(this, _isActive); }); } /// <summary> /// Initializes a new instance of the <see cref="BlowerItem"/> class. /// </summary> /// <param name="BlowerType">Type of the Blower.</param> public BlowerItem(HardwareBlowerType BlowerType) : this() { HardwareBlowerType = BlowerType; } /// <summary> /// Clones this instance. /// </summary> /// <returns></returns> public override TechItem Clone() { BlowerItem cloned = base.Clone() as BlowerItem; cloned.HardwareBlowerType = HardwareBlowerType; return cloned; } } }